iphone - iOS RestKit + 核心数据。获取缓慢且核心数据错误

标签 iphone ios web-services core-data restkit

我正在使用 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/

相关文章:

iphone - 如何使用 NSBundle 获取图像列表

ios - Xcode 11.4.1断点不适用于Pod

linux - Web 浏览器创建到本地计算机的套接字连接。

php - 如何制作 PHP SOAP 客户端并将结果 xml 存储在 php 变量中

ios - UIAlertController 前景在不应该的时候变暗

iphone - 以编程方式打开设置 iOS 6

iphone - 尽管使用 CGRectIntegral,但 UIScrollView 中的 UILabels 未对齐?

ios - 如何从谷歌地图的 gms 标记获取地址?

ios - 命名约定和针对更大 iPhone 屏幕的优化之间有什么关系?

java - S4s-elt-must-match.1:“全部”的内容必须匹配(注释?,元素*)