xCode 7.3 AsyncFetchRequestEx2
Fetch Request
1 2 3 4 5 6 7 |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass(Entity.class)]; NSLog(@"AsyncFetchRequest creation - create the predicate where acknowledged == NO"); fetchRequest.predicate = [NSPredicate predicateWithFormat:@"acknowledged == %@", @(NO)]; NSSortDescriptor * sortByRank = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES selector:@selector(caseInsensitiveCompare:)]; fetchRequest.sortDescriptors = [NSArray arrayWithObjects:sortByRank, nil]; |
Result Block
1 2 3 4 5 6 7 8 9 10 11 12 |
NSPersistentStoreAsynchronousFetchResultCompletionBlock resultBlock = ^(NSAsynchronousFetchResult *result) { NSLog(@"%s - ASYNC FETCH REQUEST DONE ! - ", __FUNCTION__); NSLog(@"-------- RESULT COUNT %lu --------",[[result finalResult] count]); //once its sorted, then we can display them [result.finalResult enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSLog(@"id: %lu, title: %@, acknowledged?: %@",(unsigned long)idx, [obj valueForKey:@"title"], [obj valueForKey:@"acknowledged"]); }]; }; |
The Async Fetch Request
1 2 3 |
NSAsynchronousFetchRequest * asyncFetch = [[NSAsynchronousFetchRequest alloc] initWithFetchRequest:fetchRequest completionBlock:resultBlock]; |
Perform the async fetch request with a context
1 2 3 4 5 6 7 8 9 10 11 12 |
NSManagedObjectContext *context = [strongSelf.coreDataCoordinator managedObjectContext]; //after setting up the async fetch request, we're gunna have the MOC execute the request by using executeRequest method [context performBlock:^{ NSLog(@"%s -- MOC performing a (Async Fetch Request) task initialized with the async fetch request and results Block-- ", __FUNCTION__); NSError *executeError = nil; //async fetch request where NSAsynchronousFetchResult * result = (NSAsynchronousFetchResult *)[context executeRequest:asyncFetch error:&executeError]; }]; |