这是我正在尝试做的简化版本:
我有一个使用 UITabBarController 作为根 Controller 的应用程序。
在两个选项卡中,我有一个 UINavigationController,其中包含一个自定义 UITableViewController。
其中一个表格 View 用于显示“特色”项目,另一个用于显示“用户”项目。
第一个 API 端点是:/api/items/featured
另一个端点是:/api/items/user
我在我的 AppDelegate.m 中完成了设置所有内容的整个过程:
// Configure the object manager
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:API_SERVER]];
objectManager.managedObjectStore = managedObjectStore;
[RKObjectManager setSharedManagerbjectManager];
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName"Item" inManagedObjectStore:managedObjectStore];
[entityMapping addAttributeMappingsFromDictionary{
@"id": @"itemID",
@"type": @"type",
@"created_at": @"created_at":
@"field1": @"field1"}];
// User Descriptor
RKResponseDescriptor *userDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping pathPattern"/api/items/user" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:userDescriptor];
// Featured Listing Decriptor
RKResponseDescriptor *featuredDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping pathPattern"/api/items/featured" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
然后在 TableViewControls 中我有以下内容(在第二个 tableview Controller 中用 'user' 代替 'featured')
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self loadItems];
}
- (void)loadItems {
[[RKObjectManager sharedManager] getObjectsAtPath"/api/items/featured" parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
self.lastupdate = [[NSDate alloc] init];
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle"OK" otherButtonTitles:nil];
[alertView show];
}];
}
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];
self.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName"Item" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey"created_at" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Set predicate to only get Featured Listings
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"type like 'featured'"];
[fetchRequest setPredicate:predicate];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
现在这一切都适用于第一个选项卡(目前它返回 8 个项目,type = 'featured',显示在表格中)。
当我切换到第二个标签时,问题就来了。它目前只返回 1 个 type='user' 的项目,并崩溃:
CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. * -[__NSArrayM objectAtIndex:]: index 3 beyond bounds for empty array with userInfo (null)
如果我在“用户”选项卡上省略谓词,则“特色”选项卡可以正常工作,“用户”选项卡会在其表中获取“特色”项目,而“用户”项目在顶部。
由于两个 tableview 控件的代码是相同的,除了“用户”和“功能”,我不知道问题出在哪里。
Best Answer-推荐答案 strong>
当您创建和配置获取的结果 Controller 时,不要使用缓存。目前他们都在使用 @"Master" 的缓存,这将导致他们尝试共享不合适的信息,因为获取请求不同。
关于ios - 在 Restkit 0.2 中加载具有相同实体和不同谓词的两个不同表时遇到问题,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/15890674/
|