0

我正在使用 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 的新手。

任何帮助将非常感激!谢谢

4

2 回答 2

1

你的方法对我来说很好。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
于 2012-11-25T10:20:43.977 回答
0

问题在于您的代码本身。您使用一些计数初始化过滤后的数组,但从未向其中添加对象。如果您将方法替换为,这将得到解决;

- (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] initWithobjects:objects];

    [self.tableView reloadData];
`]
于 2012-11-23T20:30:24.507 回答