【问题标题】:iOS RestKit + Core Data. Fetching slow and Core Data erroriOS RestKit + 核心数据。获取缓慢和核心数据错误
【发布时间】:2012-11-23 11:32:06
【问题描述】:

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

任何帮助将不胜感激! 谢谢

【问题讨论】:

    标签: iphone ios web-services core-data restkit


    【解决方案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
    

    【讨论】:

      【解决方案2】:

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

      - (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];
      `]
      

      【讨论】:

      • 我将过滤后的数组用于表格视图上的 UISearchBar。所以它只在用户搜索课程时使用。
      猜你喜欢
      • 2011-12-04
      • 2013-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多