gpt4 book ai didi

ios - 无法使用 RestKit 0.20 从核心数据中删除实体

转载 作者:可可西里 更新时间:2023-11-01 03:29:10 26 4
gpt4 key购买 nike

下面的代码不会删除实体。 “删除成功”消息出现在控制台上,因此找到了实体。我使用的所有其他操作均成功。

我正在使用 RestKit 0.20。

NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
NSError *error = nil;

NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity: [NSEntityDescription entityForName:@"Auction" inManagedObjectContext:context]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"AuctionID = %d", auctionID];
[fetchRequest setPredicate:predicate];

NSArray *result = [context executeFetchRequest:fetchRequest error:&error];
if(result.count) {
Auction *block = result[0];
[context deleteObject:block];
BOOL status = [context save:&error];
if (status == NO) {
NSLog(@"delete falied for AuctionID:%d, error: %@", auctionID, error);
}
else {
[context processPendingChanges];
NSLog(@"delete was successful for AuctionID:%d", auctionID);

}
}

为什么这个删除操作不成功,解决方法是什么。

最佳答案

我找到了这个解决方案:

事实上,您必须从持久存储中获取数据,而不是当前创建的托管上下文:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MyModel"];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"id" ascending:NO];
fetchRequest.sortDescriptors = @[descriptor];

// Setup fetched results
NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext
sectionNameKeyPath:nil
cacheName:nil];

// AND TO DELETE A MODEL :

[[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext deleteObject:myobject];

关于ios - 无法使用 RestKit 0.20 从核心数据中删除实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15096781/

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