Restkit と Core Data を使用して、Web サービスからデータをフェッチして保存しています。2 つの問題があります。1 つ目は、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];
}
ウェブで見つけたチュートリアルとそれほど変わらないと思います。
代わりに、2番目の問題は前述の方法に関連してい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 の初心者です。
どんな助けでも本当に感謝します! ありがとう