Core data RestKit和可折叠菜单,与核心数据同步
我正在使用RestKit和RRNCollapsableTable。问题是,当我第一次加载视图时,RestKit正在从JSON下载数据。该延迟导致菜单无法加载。我想做的是让CollapsableTable等待数据Core data RestKit和可折叠菜单,与核心数据同步,core-data,restkit,grand-central-dispatch,Core Data,Restkit,Grand Central Dispatch,我正在使用RestKit和RRNCollapsableTable。问题是,当我第一次加载视图时,RestKit正在从JSON下载数据。该延迟导致菜单无法加载。我想做的是让CollapsableTable等待数据 [self requestData:^(BOOL success) { if (success) { _menu = [self buildMenu]; [self model]; } }]; - (void)requestData:(v
[self requestData:^(BOOL success) {
if (success) {
_menu = [self buildMenu];
[self model];
}
}];
- (void)requestData:(void (^)(BOOL success))completionBlock {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Messages"];
NSSortDescriptor *byId = [NSSortDescriptor sortDescriptorWithKey:@"customNewsId" ascending:YES];
fetchRequest.sortDescriptors = @[byId];
NSError *error = nil;
// Setup fetched results
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext
sectionNameKeyPath:@"customNewsId"
cacheName:nil];
//[self.fetchedResultsController setDelegate:self];
BOOL fetchSuccessful = [self.fetchedResultsController performFetch:&error];
if (!fetchSuccessful) {
abort();
}
[[RKObjectManager sharedManager] getObjectsAtPath:@"api/json/get/bZmroLaBCG" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Load complete: Table should refresh...");
//[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"LastUpdatedAt"];
//[[NSUserDefaults standardUserDefaults] synchronize];
NSError *error = nil;
if ([[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext hasChanges] && ![[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext saveToPersistentStore:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
completionBlock(YES);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Load failed with error: %@", error);
completionBlock(NO);
}];
completionBlock(YES);
}
然后BuildMenu只是对获取的对象进行迭代,并将它们放在节中
-(NSArray *)buildMenu {
__block NSMutableArray *collector = [NSMutableArray new];
NSInteger sections = [self.fetchedResultsController.sections count];
for (NSInteger i = 0; i < sections; i++) {
Messages *message = [_fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:i]];
MenuSection *section = [MenuSection new];
section.items = @[message.message,message.pushNotificationMessage];
section.title = message.title;
NSLog(@"section.title - %@",section.title);
[collector addObject:section];
}
return [collector copy];
}
提前感谢您的帮助
您好。所以在完成块中构建菜单?我就是这么做的,但看起来完成块并不等待成功块…我没有看到它在块中被调用。。。
-(NSArray *)model {
return _menu;
}