cs193p – Assignment #6 Task #2 Part #3

Please note, this blog entry is from a previous course. You might want to check out the current one.

Recreate the user-interface from last week’s assignment but rely entirely on the above-mentioned Core Data database to present it.

Copy the files for the last table view controller into the current project. Adjust it to setup the fetched results controller every time the view appears:

- (void)viewWillAppear:(BOOL)animated
{
    ...
    [self.sh useDocumentWithOperation:^(BOOL success) {
        [self setupFetchedResultsController];
    }];
}


Like before the shared document handler is instantiated lazily:

- (SharedDocumentHandler *)sh
{
    if (!_sh) {
        _sh = [SharedDocumentHandler sharedDocumentHandler];
    }
    return _sh;
}

The fetched-results controller looks for photos with an attached Recent entity, and sorts the results by date:

- (void)setupFetchedResultsController
{
    if (self.sh.managedObjectContext) {
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photo"];
        request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"recent.lastViewed"
                                                                  ascending:NO]];
        request.predicate = [NSPredicate predicateWithFormat:@"recent != nil"];
        self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                            managedObjectContext:self.sh.managedObjectContext
                                                                              sectionNameKeyPath:nil
                                                                                       cacheName:nil];
    } else {
        self.fetchedResultsController = nil;
    }
}

Running the app in the current state, you will notice, that the recent-photos table is not updated. To do this it is necessary to force saving the document every time its contents change. Add a new method to the the shared document handler:

- (void)saveDocument
{
    [self.document saveToURL:self.document.fileURL
            forSaveOperation:UIDocumentSaveForOverwriting
           completionHandler:NULL];
}

… which is using a new property, instantiated lazily and which also can be used in the other method:

- (UIManagedDocument *)document
{
    if (!_document) {
        NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
                                                             inDomains:NSUserDomainMask] lastObject];
        url = [url URLByAppendingPathComponent:@"SPoTDocument"];
        _document = [[UIManagedDocument alloc] initWithFileURL:url];
    }
    return _document;
}

- (void)useDocumentWithOperation:(void (^)(BOOL))block
{
    UIManagedDocument *document = self.document;
    if (![[NSFileManager defaultManager] fileExistsAtPath:[document.fileURL path]]) {
        [document saveToURL:document.fileURL
           ...
}

Use this new method when saving the photos loaded from Flickr:

- (void)loadPhotosFromFlickr
{
            ...
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.refreshControl endRefreshing];
                [self.sh saveDocument];
            });
        }];
    });
}

… and when changing the Recent entities:

- (void)sendDataforIndexPath:(NSIndexPath *)indexPath toViewController:(UIViewController *)vc
{
        ...
        [Recent recentPhoto:photo];
        [[SharedDocumentHandler sharedDocumentHandler] saveDocument];
        ...
}

Finally copy the complete story board for the iPad from the previous project to the current one.

The complete code is available on github.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

Leave a Reply

Your email address will not be published. Required fields are marked *