Database Reference
In-Depth Information
index = [self indexOfEntryWithFileURL:fileURL];
[self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]]];
} else {
CTEntry *entry = [_entries objectAtIndex:index];
[entry setMetadata:metadata];
[entry setState:state];
[entry setVersion:version];
[_entries sortUsingComparator:^NSComparisonResult(CTEntry *entry1, CTEntry *entry2) {
NSComparisonResult result = [[[entry1 metadata] displayName]
compare:[[entry2 metadata] displayName]];
NSLog(@"results is %d",result);
return result;
}];
NSInteger newIndex = [self indexOfEntryWithFileURL:fileURL];
if(index != newIndex){
[self.collectionView moveItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]
toIndexPath:[NSIndexPath indexPathForRow:newIndex inSection:0]];
}
[self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:newIndex
inSection:0]]];
}
}
We start off by finding the index of the
CTEntry
with the
fileURL
by calling
indexOfEntryWithFileURL:
.
This is a helper method that we will write shortly. If the index is equal to
NSNotFound,
then we know that
this item does not exist in the entries array so it is a new document.
If it is a new document, we create a new
CTEntry
by calling
initWithFileURL:metadata:stat
e:andVersion:
. Then we add the new entry to our entries array. We then call an array method
sortUsingComparator:
. Inside the comparator block we compare the display names of each object
and return the
NSComparisonResult
. We end up with an alphabetically sorted array. Because we
sorted the array, we call the
indexOfEntryWithFileURL:
method again to determine our current
index. Now that we know exactly where this new object is we call
insertItemsAtIndexPaths:
on our
Collection View. This adds the object to our view.
If we are referencing a current document, we get the
CTEntry
by assigning what is at the given index
in our entries array. We set the metadata, state, and version of the
CTEntry
to make sure it is current.
We then perform a sort exactly like we did earlier to alphabetize the array. Next we set a
newIndex
variable by calling
indexOfEntryWithFileURL:
. Now that we have a new index we check to see
whether our
newIndex
and index are not the same. If they aren't, then we now that we need to move
our object. We do this by calling
moveItemAtIndexPath:toIndexPath:
against our Collection View.
Finally, we call the method
reloadItemsAtIndexPaths:
and pass it an array with our single index path.
Now let's add our helper method
indexOfEntryWithFileURL:
.
-(NSInteger)indexOfEntryWithFileURL:(NSURL *)fileURL {
__block NSInteger index = NSNotFound;
Search WWH ::
Custom Search