我正在使用 Restkit 和 Core Data 从 Web 服务中获取和存储数据。我有两个问题。第一个是获取 3200 条记录大约需要 10 秒。我想它不应该这么慢。这是我的代码:
- (void)fetchDataFromRemote{
RKManagedObjectMapping *coursesMapping = [RKManagedObjectMapping mappingForClass:[Course class] inManagedObjectStore:[[RKObjectManager sharedManager] objectStore]];
[coursesMapping mapKeyPathsToAttributes:@"code", @"code",@"name",@"name", nil];
//Set the primary key. Records in this way are not duplicated when fetched
coursesMapping.primaryKeyAttribute = @"code";
[[[RKObjectManager sharedManager] mappingProvider] setMapping:coursesMapping forKeyPath:@"course"];
RKManagedObjectMapping *cacheMapping = [RKManagedObjectMapping mappingForClass:[Cache class] inManagedObjectStore:[[RKObjectManager sharedManager] objectStore]];
[cacheMapping mapKeyPathsToAttributes:@"updated",@"updated",@"expires",@"expires", nil];
//Set the primary key. Records in this way are not duplicated when fetched
cacheMapping.primaryKeyAttribute = @"expires";
[coursesMapping mapRelationship:@"cache" withMapping:cacheMapping];
[[[RKObjectManager sharedManager] mappingProvider] setMapping:cacheMapping forKeyPath:@"cache"];
RKURL *URL = [RKURL URLWithBaseURL:[[RKObjectManager sharedManager] baseURL] resourcePath:@"/course/-" queryParameters:nil];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@", [URL resourcePath]] delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSLog(@"Start:%@",[NSDate date]);
}
当我收到来自服务器的响应时:
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
NSLog(@"End:%@",[NSDate date]);
//Dismiss the activity indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(@"objects[%d]", [objects count]);
self.coursesArray = objects;
//Initialize the filtered array
self.filteredCoursesArray = [[NSMutableArray alloc] initWithCapacity:[self.coursesArray count]];
[self.tableView reloadData];
}
我想这与我在网上找到的教程没有什么不同。
第二个问题与上述方法有关fetchDataFromRemote:
当我添加这串行时:
RKManagedObjectMapping *cacheMapping = [RKManagedObjectMapping mappingForClass:[Cache class] inManagedObjectStore:[[RKObjectManager sharedManager] objectStore]];
[cacheMapping mapKeyPathsToAttributes:@"updated",@"updated",@"expires",@"expires", nil];
//Set the primary key. Records in this way are not duplicated when fetched
cacheMapping.primaryKeyAttribute = @"expires";
[coursesMapping mapRelationship:@"cache" withMapping:cacheMapping];
[[[RKObjectManager sharedManager] mappingProvider] setMapping:cacheMapping forKeyPath:@"cache"];
我遇到了一个例外,tableview:cellForRowAtIndexPath:
因为该对象似乎不再是“课程”对象而是“缓存”对象,因此向“缓存”对象不知道的选择器发送消息会使应用程序崩溃。如果我在一切正常之前删除线条,但我需要设置这种关系。我不确定我是否一切正常,我是 Core Data 的新手。
任何帮助将非常感激!谢谢