간헐적 인 문제가 있습니다. 시뮬레이터에서 앱을 삭제하고 Xcode를 실행합니다. 앱이 처음으로 실행됩니다. RESTKit은 서버에서 7 개의 개체를 다운로드합니다 (아래 로그). 그러나 NSFetchedResultsController는 섹션 개수 == 0을 표시하고 테이블 뷰는 비어 있습니다.
2014-06-06 07:14:22.541 App[4859:60b] I restkit.network:RKObjectRequestOperation.m:180 GET 'http://www.domain.com/meetings?lastrequest=2004-06-06T07%3A00%3A00Z'
2014-06-06 07:14:22.772 App[4859:7b03] I restkit.network:RKObjectRequestOperation.m:250 GET 'http://www.domain.com/meetings?lastrequest=2004-06-06T07%3A00%3A00Z' (200 OK / 7 objects) [request=0.1917s mapping=0.1528s total=0.2559s]
앱을 중지하고 다시 시작하면 tableView에 3 개의 섹션과 7 개의 셀이 표시됩니다. NSFetchedResultsController가 처음 업데이트되지 않는 것과 같습니다. 앱이 처음에 데이터를 임의로로드하는 경우가 있습니다.
다음은 내 FRC 설정입니다.
- (NSFetchedResultsController *)fetchedResultsController
{
if(_fetchedResultsController!=nil)
{
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Meetings"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *firstSort = [[NSSortDescriptor alloc] initWithKey:@"startDate"
ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:firstSort,nil];
[fetchRequest setSortDescriptors:sortDescriptors];
self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:@"sectionIdentifier"
cacheName:nil];
self.fetchedResultsController.delegate = self;
return self.fetchedResultsController;
}
FRC 대표자 :
#pragma mark - NSFetchedControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView reloadData];
[self.tableView endUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
switch (type)
{
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
{
Invite *changedInvite = [self.currentFRC objectAtIndexPath:indexPath];
STInviteSummaryCell *cell = (STInviteSummaryCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[cell updateValuesForChangedInvite:changedInvite];
}
break;
case NSFetchedResultsChangeMove:
{
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
RESTKit 가져 오기 :
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];
operation.managedObjectContext = self.objectManager.managedObjectStore.mainQueueManagedObjectContext;
operation.managedObjectCache = appDelegate.managedObjectStore.managedObjectCache;
operation.savesToPersistentStore = NO;
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
//Before saving the objects, here I iterate through the each object in the array and flag each attribute that's changed in the object.
NSError *error;
if ([self.managedObjectContext saveToPersistentStore:&error])
{
NSLog (@"******* OBJECTS SAVED **********");
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
[self updateUI];
});
}];
[operation start];
[operation waitUntilFinished];
}
다시 한 번 문제는 간헐적입니다.
편집하다
같은 클래스에 다음 코드가 있습니다.
- (void)loadView
{
[self getManagedObjectFromAppDelegate];
[self registerForReachibilityNotificaiton];
[self fetchInvitesInBackgroundThread];
[super loadView];
}
- (void)getManagedObjectFromAppDelegate
{
STAppDelegate *appDelegate = (STAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate setupCoreDataWithRESTKit];
self.objectManager = [self getObjectManager];
self.objectManager.managedObjectStore = appDelegate.managedObjectStore;
self.objectManager.managedObjectStore.managedObjectCache = appDelegate.managedObjectStore.managedObjectCache;
self.managedObjectContext = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
}
관리되는 개체 컨텍스트가 올바르게 연결되지 않은 것처럼 보이므로 mainQueueManagedObjectContext
모든 것에 사용할 수 있습니다 . 작업을 실행할 다른 자식 컨텍스트를 만들 수 있지만 프로파일 링에 성능 문제가 있음이 표시 될 때까지 복잡성을 추가 할 필요가 없습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다