Database Reference
In-Depth Information
[_entries enumerateObjectsUsingBlock:^(CTEntry *entry, NSUInteger idx, BOOL *stop) {
if([[entry fileURL] isEqual:fileURL]){
index = idx;
*stop = YES;
}
}];
return index;
}
As you can see, this method is very simple. We start off by setting our index to NSNotFound . Then we
enumerate through our entries using the method enumerateObjectsUsingBlock: . We compare our
fileURL with the entry fileURL . If they are equal, we set the index and tell the enumeration to stop.
Finally, we return the index. Simple and sweet.
Now let's modify a few methods that we created earlier. Let's start with collectionView: cellForItemAt
IndexPath: . We need to change the line where we get the entry and assign it to a type NSString and
now assign it to a type CTEntry .
CTEntry *entry = _entries[indexPath.row];
Now in our collectionView:didSelectItemAtIndexPath: we need to change it to look like this.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)
indexPath {
CTEntry *entry = _entries[indexPath.row];
_selectedDocument = [[CTDocument alloc] initWithFileURL:[entry fileURL]];
_shouldStartEditing = NO;
[_selectedDocument openWithCompletionHandler:^(BOOL success) {
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"ToFriendDetails" sender:nil];
});
}];
}
The first thing we do is change the type of entry to CTEntry . Then we initialize a CTDocument with
the fileURL from our entry and assign it to selectedDocument. Because this file already exists,
we set shouldStartEditing to NO . Then we call the method openWithCompletionHandler : on our
selectedDocument. This method is handled on a background thread so when it is complete we call
our performSegueWithIdentifier : method inside a dispatch_async call passing it the
dispatch_get_main_queue() method as the queue parameter.
Now we need to modify our deleteEntry: method to look like the following:
-(void)deleteEntry:(CTEntry *)entry {
NSFileManager *fileManager = [[NSFileManager alloc] init];
[fileManager removeItemAtURL:[entry fileURL] error:nil];
[self removeEntryWithURL:[entry fileURL]];
}
 
Search WWH ::




Custom Search