我正在经历一个非常奇怪的iOS核心数据行为,我正在从Web服务下载项目并使用核心数据在本地存储它,我有一个UTTableViewController,显示存储在本地实体中的内容,同样的tableController也允许用于删除对所选项目的操作。 我也对属性“itemId”有一个约束以避免重复,如果itemId是重复的话我使用默认行为抛出错误
问题如下
当我第一次从网络服务下载项目时,它完全按预期工作。
如果我尝试重新加载数据,则约束会正确显示存在冲突的错误。
然后我进入UITableViewController并删除其中一个项目,这可以正常运行ViewCOntroller显示其余项目。
现在我从Web服务重新加载项目,现在它无法添加已删除的项目,约束告诉我存在冲突。
就好像该项目并未真正删除,仍然在后台挂起但不可见。
调试显示所有看起来都很好
当我重新加载我删除的相同itemId时,有人可以帮我解释为什么我收到约束错误。
这是下载方法
+ (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)"
)}
删除代码如下所示,删除完成时没有显示错误
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = self.persistentContainer.viewContext;
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[context deleteObject:[self.pimItems objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error]) {
if([NWTillHelper isDebug] == 1) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}
}
// Remove device from table view
[self.pimItems removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
任何帮助都会非常有帮助
------编辑------
我从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的数组是空的,而fetch请求是NULL,所以持久存储是空的,如果它为空,为什么我不能重新加载数据呢?
这是填充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];
}
答案 0 :(得分:1)
问题是我没有在所有地方始终使用appDelegate
WebServices类的以下行必须位于所有地方
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
感谢Vinodh和Flexicoder指出