gpt4 book ai didi

ios - 在 Restkit 0.2 中加载具有相同实体和不同谓词的两个不同表时遇到问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:14:40 27 4
gpt4 key购买 nike

这是我正在尝试做的简化版本:

我有一个使用 UITabBarController 作为根 Controller 的应用程序。在两个选项卡中,我有一个包含自定义 UITableViewController 的 UINavigationController。

其中一个 Tableview 用于显示“特色”项目,另一个用于显示“用户”项目。

第一个的 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 setSharedManager:objectManager];

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 个类型为“用户”的项目,并且崩溃:

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 控件的代码是相同的,除了“user”和“featured”,我不知道问题出在哪里。

最佳答案

当您创建和配置您的抓取结果 Controller 时,不要使用缓存。目前他们都在使用 @"Master" 的缓存,这将导致他们尝试共享不合适的信息,因为获取请求不同。

关于ios - 在 Restkit 0.2 中加载具有相同实体和不同谓词的两个不同表时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15890674/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com