Xcode 8.1 IOS10.1 CoreData delete不会真正删除
我在使用iOS核心数据时遇到了一种非常奇怪的行为,我从web服务下载项目并使用核心数据将其存储在本地,我有一个UTTableViewController,显示本地实体中存储的内容,同一个tableController还允许对所选项目执行删除操作。 我对属性“itemId”也有一个约束以避免重复,如果itemId是重复的,我使用默认行为抛出一个错误 问题如下 当我第一次从webservice下载项目时,它完全按照预期工作 如果我尝试重新加载数据,约束将正确显示存在冲突的错误 然后我进入UITableViewController并删除其中一个项目,这与预期的一样,ViewCOntroller会显示其余的项目 现在我从web服务重新加载项目,现在它无法添加回已删除的项目,约束告诉我存在冲突 这就好像该项目没有真正删除,仍然挂在背景中,但不可见 调试告诉我,一切看起来都很好 有人能帮我解释一下,当我重新加载删除的同一个itemId时,为什么会出现约束错误 这是下载方法Xcode 8.1 IOS10.1 CoreData delete不会真正删除,ios,objective-c,core-data,Ios,Objective C,Core Data,我在使用iOS核心数据时遇到了一种非常奇怪的行为,我从web服务下载项目并使用核心数据将其存储在本地,我有一个UTTableViewController,显示本地实体中存储的内容,同一个tableController还允许对所选项目执行删除操作。 我对属性“itemId”也有一个约束以避免重复,如果itemId是重复的,我使用默认行为抛出一个错误 问题如下 当我第一次从webservice下载项目时,它完全按照预期工作 如果我尝试重新加载数据,约束将正确显示存在冲突的错误 然后我进入UITabl
+ (void)fetchTillData:(int)tillId; {
if ([NWTillHelper isDebug] == 1) {
NSLog(@"WebServices:fetchTillData:tillId = %d", tillId);
}
NSString *finalURL = [NSString stringWithFormat:@"https://foo.bar.com:5443/api/till/tilldata/%d?StartAtRow=0&TakeNoOfRows=10",tillId];
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:finalURL]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error != nil) {
if ([NWTillHelper isDebug] == 1) {
NSLog(@"WebServices:fetchTillData:Transport error %@", error);
}
} else {
NSHTTPURLResponse *responseHTTP;
responseHTTP = (NSHTTPURLResponse *) response;
if(responseHTTP.statusCode != 200) {
if ([NWTillHelper isDebug] == 1) {
NSLog(@"WebServices:fetchTillData:Server Error %d", (int) responseHTTP.statusCode);
}
} else {
NSArray *tillBasicDataArray = [NSJSONSerialization JSONObjectWithData:data
options:0
error:NULL];
if ([NWTillHelper isDebug] == 1) {
NSLog(@"WebServices:fetchTillData:tillBasicDataArray count = %lu", (unsigned long)[tillBasicDataArray count]);
NSLog(@"WebServices:fetchTillData:tillBasicDataArray looks like %@",tillBasicDataArray);
}
NSDictionary *tillBasicDataDict = Nil;
//Loop through the array and for each dictionary insert into local DB
for (id element in tillBasicDataArray){
tillBasicDataDict = element;
NSString *itemId = [tillBasicDataDict objectForKey:@"itemId"];
NSString *companyId = [tillBasicDataDict objectForKey:@"companyId"];
NSString *languageId = [tillBasicDataDict objectForKey:@"languageCode"];
NSString *colorCode = [tillBasicDataDict objectForKey:@"colorCode"];
NSString *discountable = [tillBasicDataDict objectForKey:@"discountable"];
NSString *exchangeable = [tillBasicDataDict objectForKey:@"exchangeable"];
NSString *noos14 = [tillBasicDataDict objectForKey:@"noos14"];
NSString *sizeCode = [tillBasicDataDict objectForKey:@"sizeCode"];
NSString *taxGroup = [tillBasicDataDict objectForKey:@"taxGroupId"];
NSString *taxRegion = [tillBasicDataDict objectForKey:@"taxRegion"];
NSString *tradeItemDesc = [tillBasicDataDict objectForKey:@"tradeItemDesc"];
NSString *withTax = [tillBasicDataDict objectForKey:@"withTax"];
NSString *status = [tillBasicDataDict objectForKey:@"status"];
// Use Core Data FMD
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
//AppDelegate *appDelegate =
//[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
appDelegate.persistentContainer.viewContext;
NSManagedObject *newPimItem = Nil;
newPimItem = [NSEntityDescription
insertNewObjectForEntityForName:@"TillData"
inManagedObjectContext:context];
[newPimItem setValue:itemId forKey:@"itemId"];
[newPimItem setValue:companyId forKey:@"companyId"];
[newPimItem setValue:languageId forKey:@"languageCode"];
[newPimItem setValue:colorCode forKey:@"colorCode"];
[newPimItem setValue:discountable forKey:@"discountable"];
[newPimItem setValue:exchangeable forKey:@"exchangeable"];
[newPimItem setValue:noos14 forKey:@"noos14"];
[newPimItem setValue:sizeCode forKey:@"sizeCode"];
[newPimItem setValue:[NSNumber numberWithInt:[taxGroup intValue]] forKey:@"taxGroup"];
[newPimItem setValue:taxRegion forKey:@"taxRegion"];
[newPimItem setValue:tradeItemDesc forKey:@"tradeItemDesc"];
[newPimItem setValue:[NSNumber numberWithInt:[withTax intValue]] forKey:@"withTax"];
[newPimItem setValue:[NSNumber numberWithInt:[status intValue]] forKey:@"status"];
NSError *error = Nil;
[context save:&error];
if ([NWTillHelper isDebug] == 1) {
NSLog(@"WebServices:fetchTillData:ItemId in loop = %@", itemId);
NSLog(@"WebServices:fetchTillData:newPimItem = %@", newPimItem);
NSLog(@"WebServices:fetchTillData:CoreData error = %@", error);
}
if(error != nil) {
// Do something here
} else {
NSUserDefaults *tillUserDefaults = [NSUserDefaults standardUserDefaults];
[tillUserDefaults setInteger:1 forKey:@"hasTillData"];
[tillUserDefaults synchronize];
}
}
}
}
}] resume];
}
调试告诉我,我在任何地方都没有任何NIL/NULL值
调试还向我显示,每个变量都保存着期望值
下面是使用NSLog从webservice获得的JSON数组,所有值都是正确的
WebServices:fetchTillData:tillBasicDataArray looks like (
{
colorCode = 95;
companyId = "BE_HM";
discountable = 1;
exchangeable = 1;
itemId = 101064025138010;
languageCode = eng;
noos14 = "09258384374953,09258387354952";
sizeCode = "163-010";
status = 1;
taxGroupId = 1;
taxRegion = BE;
tradeItemDesc = "Jersey basic";
withTax = 1;
},
{
colorCode = 95;
companyId = "BE_HM";
discountable = 1;
exchangeable = 1;
itemId = 101064025138011;
languageCode = eng;
noos14 = "09258384394951,09258387434951";
sizeCode = "163-011";
status = 1;
taxGroupId = 1;
taxRegion = BE;
tradeItemDesc = "Jersey basic";
withTax = 1;
},
{
colorCode = 95;
companyId = "BE_HM";
discountable = 1;
exchangeable = 1;
itemId = 101064025138012;
languageCode = eng;
noos14 = "09258385254957,09258389874953";
sizeCode = "163-012";
status = 1;
taxGroupId = 1;
taxRegion = BE;
tradeItemDesc = "Jersey basic";
withTax = 1;
}
)
但是,即使删除了所有条目并且tableViewController显示我的数据库为空,我仍然会收到此错误
2016-11-23 15:07:36.714 NWMobileTill[674:9231] WebServices:fetchTillData:ItemId in loop = 101064025138010
2016-11-23 15:07:36.715 NWMobileTill[674:9231] WebServices:fetchTillData:newPimItem = <TillData: 0x6000002ce9a0> (entity: TillData; id: 0x600000624620 <x-coredata:///TillData/t7807C999-528C-4616-A012-592A3D77D28D15> ; data: {
colorCode = 95;
companyId = "BE_HM";
discountable = 1;
exchangeable = 1;
itemId = 101064025138010;
languageCode = eng;
noos14 = "09258384374953,09258387354952";
sizeCode = "163-010";
status = 1;
taxGroup = 1;
taxRegion = BE;
tradeItemDesc = "Jersey basic";
withTax = 1;
})
2016-11-23 15:07:36.717 NWMobileTill[674:9231] WebServices:fetchTillData:CoreData error = Error Domain=NSCocoaErrorDomain Code=133021 "(null)" UserInfo={conflictList=(
"NSConstraintConflict (0x60000086dcc0) for constraint (\n itemId\n): database: (null), conflictedObjects: (\n \"<TillData: 0x6000000de4c0> (entity: TillData; id: 0x600000435500 <x-coredata:///TillData/t7807C999-528C-4616-A012-592A3D77D28D7> ; data: {\\n colorCode = 95;\\n companyId = \\\"BE_HM\\\";\\n discountable = 1;\\n exchangeable = 1;\\n itemId = 101064025138012;\\n languageCode = eng;\\n noos14 = \\\"09258385254957,09258389874953\\\";\\n sizeCode = \\\"163-012\\\";\\n status = 1;\\n taxGroup = 1;\\n taxRegion = BE;\\n tradeItemDesc = \\\"Jersey basic\\\";\\n withTax = 1;\\n})\",\n \"<TillData: 0x6000002ce3f0> (entity: TillData; id: 0x60000023bda0 <x-coredata:///TillData/t7807C999-528C-4616-A012-592A3D77D28D13> ; data: {\\n colorCode = 95;\\n companyId = \\\"BE_HM\\\";\\n discountable = 1;\\n exchangeable = 1;\\n itemId = 101064025138012;\\n languageCode = eng;\\n noos14 = \\\"09258385254957,09258389874953\\\";\\n sizeCode = \\\"163-012\\\";\\n status = 1;\\n taxGroup = 1;\\n taxRegion = BE;\\n tradeItemDesc = \\\"Jersey basic\\\";\\n withTax = 1;\\n})\",\n \"<TillData: 0x6080002cfc00> (entity: TillData; id: 0x60800022f920 <x-coredata:///TillData/t7807C999-528C-4616-A012-592A3D77D28D4> ; data: {\\n colorCode = 95;\\n companyId = \\\"BE_HM\\\";\\n discountable = 1;\\n exchangeable = 1;\\n itemId = 101064025138012;\\n languageCode = eng;\\n noos14 = \\\"09258385254957,09258389874953\\\";\\n sizeCode = \\\"163-012\\\";\\n status = 1;\\n taxGroup = 1;\\n taxRegion = BE;\\n tradeItemDesc = \\\"Jersey basic\\\";\\n withTax = 1;\\n})\"\n)",
"NSConstraintConflict (0x60000067fd40) for constraint (\n itemId\n): database: (null), conflictedObjects: (\n \"<TillData: 0x6080002d0760> (entity: TillData; id: 0x608000233320 <x-coredata:///TillData/t7807C999-528C-4616-A012-592A3D77D28D6> ; data: {\\n colorCode = 95;\\n companyId = \\\"BE_HM\\\";\\n discountable = 1;\\n exchangeable = 1;\\n itemId = 101064025138011;\\n languageCode = eng;\\n noos14 = \\\"09258384394951,09258387434951\\\";\\n sizeCode = \\\"163-011\\\";\\n status = 1;\\n taxGroup = 1;\\n taxRegion = BE;\\n tradeItemDesc = \\\"Jersey basic\\\";\\n withTax = 1;\\n})\",\n \"<TillData: 0x6080002cfb20> (entity: TillData; id: 0x60800022f9e0 <x-coredata:///TillData/t7807C999-528C-4616-A012-592A3D77D28D3> ; data: {\\n colorCode = 95;\\n companyId = \\\"BE_HM\\\";\\n discountable = 1;\\n exchangeable = 1;\\n itemId = 101064025138011;\\n languageCode = eng;\\n noos14 = \\\"09258384394951,09258387434951\\\";\\n sizeCode = \\\"163-011\\\";\\n status = 1;\\n taxGroup = 1;\\n taxRegion = BE;\\n tradeItemDesc = \\\"Jersey basic\\\";\\n withTax = 1;\\n})\",\n \"<TillData: 0x6080002d3be0> (entity: TillData; id: 0x608000221ea0 <x-coredata:///TillData/t7807C999-528C-4616-A012-592A3D77D28D12> ; data: {\\n colorCode = 95;\\n companyId = \\\"BE_HM\\\";\\n discountable = 1;\\n exchangeable = 1;\\n itemId = 101064025138011;\\n languageCode = eng;\\n noos14 = \\\"09258384394951,09258387434951\\\";\\n sizeCode = \\\"163-011\\\";\\n status = 1;\\n taxGroup = 1;\\n taxRegion = BE;\\n tradeItemDesc = \\\"Jersey basic\\\";\\n withTax = 1;\\n})\"\n)",
"NSConstraintConflict (0x60000067fc00) for constraint (\n itemId\n): database: (null), conflictedObjects: (\n \"<TillData: 0x6080002cecb0> (entity: TillData; id: 0x6080002218e0 <x-coredata:///TillData/t7807C999-528C-4616-A012-592A3D77D28D5> ; data: {\\n colorCode = 95;\\n companyId = \\\"BE_HM\\\";\\n discountable = 1;\\n exchangeable = 1;\\n itemId = 101064025138010;\\n languageCode = eng;\\n noos14 = \\\"09258384374953,09258387354952\\\";\\n sizeCode = \\\"163-010\\\";\\n status = 1;\\n taxGroup = 1;\\n taxRegion = BE;\\n tradeItemDesc = \\\"Jersey basic\\\";\\n withTax = 1;\\n})\",\n \"<TillData: 0x6080002cf2d0> (entity: TillData; id: 0xd000000000180002 <x-coredata://6A608EA5-F8C8-4272-8C10-8621BB730066/TillData/p6> ; data: {\\n colorCode = 95;\\n companyId = \\\"BE_HM\\\";\\n discountable = 1;\\n exchangeable = 1;\\n itemId = 101064025138010;\\n languageCode = eng;\\n noos14 = \\\"09258384374953,09258387354952\\\";\\n sizeCode = \\\"163-010\\\";\\n status = 1;\\n taxGroup = 1;\\n taxRegion = BE;\\n tradeItemDesc = \\\"Jersey basic\\\";\\n withTax = 1;\\n})\",\n \"<TillData: 0x6000002ce9a0> (entity: TillData; id: 0x600000624620 <x-coredata:///TillData/t7807C999-528C-4616-A012-592A3D77D28D15> ; data: {\\n colorCode = 95;\\n companyId = \\\"BE_HM\\\";\\n discountable = 1;\\n exchangeable = 1;\\n itemId = 101064025138010;\\n languageCode = eng;\\n noos14 = \\\"09258384374953,09258387354952\\\";\\n sizeCode = \\\"163-010\\\";\\n status = 1;\\n taxGroup = 1;\\n taxRegion = BE;\\n tradeItemDesc = \\\"Jersey basic\\\";\\n withTax = 1;\\n})\",\n \"<TillData: 0x6080002d1e20> (entity: TillData; id: 0x608000229160 <x-coredata:///TillData/t7807C999-528C-4616-A012-592A3D77D28D11> ; data: {\\n colorCode = 95;\\n companyId = \\\"BE_HM\\\";\\n discountable = 1;\\n exchangeable = 1;\\n itemId = 101064025138010;\\n languageCode = eng;\\n noos14 = \\\"09258384374953,09258387354952\\\";\\n sizeCode = \\\"163-010\\\";\\n status = 1;\\n taxGroup = 1;\\n taxRegion = BE;\\n tradeItemDesc = \\\"Jersey basic\\\";\\n withTax = 1;\\n})\"\n)"
)}
任何帮助都会很有帮助
------编辑------
从tableView中删除并关闭应用程序后,重新启动应用程序,我可以按预期再次加载数据,但只要我停留在应用程序中导航到应用程序的不同部分,然后返回,我就无法加载数据,即使调试告诉我,当我进入tableView时,它是空的,但当我尝试加载数据时,我会收到错误
显示持久存储为空的调试
2016-11-23 16:09:04.106 NWMobileTill[1554:32365] pimItemsArray holds (
)
2016-11-23 16:09:04.106 NWMobileTill[1554:32365] fetchReuestError holds (null)
正如您所看到的,支持tableview的数组是空的,获取请求是空的,因此持久存储是空的,如果它是空的,为什么我不能重新加载数据
这是填充tableView的代码
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSError *error = nil;
// Fetch the devices from persistent data store
NSManagedObjectContext *context = self.persistentContainer.viewContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"TillData"];
self.pimItems = [[context executeFetchRequest:fetchRequest error:&error] mutableCopy];
if([NWTillHelper isDebug] == 1) {
NSLog(@"pimItemsArray holds %@", self.pimItems);
NSLog(@"fetchReuestError holds %@", error);
}
[self.tableView reloadData];
}
问题是我没有在所有地方都一致地使用appDelegate WebServices类中的以下行需要位于所有位置
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
感谢Vinodh和Flexicoder指出这一点对不起,我不确定你的意思,我阅读了链接,我正在做同样的事情,只是没有在NSManagedObject上循环,因为我只担心表中的一行。因此,我调用deleteObject,然后保存上下文,该上下文应该从持久存储中删除该对象。如果我在表中执行删除操作,退出应用程序,启动应用程序,则它可以工作,但如果我留在应用程序中,返回主菜单,然后返回管理菜单以加载数据,则它无法工作。我无法读取它们,就我所见,它们不在那里,下面的调试确认,当我加载Tableview并从持久性存储中读取其空值时,
appDelegate.persistentContainer.viewContext
和self.persistentContainer.viewContext
指向相同的context
?
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];