我正在使用 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:
中遇到异常,因为该对象似乎不再是“类(class)”对象,而是一个“缓存”对象,因此向选择器发送一条消息,该选择器“缓存对象不知道会导致应用程序崩溃。如果我在一切正常之前删除这些行,但我需要设置该关系。我不确定我是否已完成所有操作,我是 Core Data 的新手。
任何帮助将不胜感激! 谢谢
最佳答案
你的方法看起来很适合我。 3200 个对象的 10 秒持续时间似乎相当慢,但这取决于您的映射和关系。
如果您想在导入过程中检查任何错误,您可以 1) 在您的方案中启用 SQL 输出,或者 2) 记录 RestKit 提供的所有输出以确定导入过程中的错误,这会减慢进度。
1) -com.apple.CoreData.SQLDebug 1 // Add in "Arguments" Tab > "Arguments Passed On Launch"
2) RKLogConfigureByName("RestKit/*", RKLogLevelTrace); // Add to AppDelegate
关于iphone - iOS RestKit + 核心数据。获取缓慢且核心数据错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13528223/