首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xcode8.1 IOS10.1 CoreData delete不是真正的delete

Xcode8.1 IOS10.1 CoreData delete不是真正的delete
EN

Stack Overflow用户
提问于 2016-11-23 15:27:01
回答 1查看 116关注 0票数 1

对于iOS核心数据,我遇到了一个非常奇怪的行为,我正在从web服务下载项目并使用核心数据将其存储在本地,我有一个UTTableViewController,它显示存储在本地实体中的内容,相同的tableController还允许对选定项目执行删除操作。我在属性" itemId“上也有一个约束,以避免重复,如果itemId是重复的,我使用默认行为抛出错误

问题如下

当我第一次从When服务下载项目时,它完全按照预期的方式工作。

如果我尝试重新加载数据,约束会正确地显示存在冲突的错误。

然后,我进入UITableViewController并删除其中一项,这按预期工作,ViewCOntroller会显示剩余的项。

现在我从web服务重新加载项目,现在它不能添加被删除的项目,约束告诉我存在冲突。

这就好像项目并没有被真正删除,仍然在后台徘徊,但看不见。

Debugs告诉我一切看起来都很好

有人能帮我解释一下为什么当我重新加载我删除的同一个itemId时会出现约束错误吗?

这是下载方法

代码语言:javascript
复制
+ (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数组,所有值都是正确的

代码语言:javascript
复制
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显示我的数据库为空之后,我仍然收到此错误

代码语言:javascript
复制
    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)"
)}

删除代码如下所示,删除完成后不会显示任何错误

代码语言:javascript
复制
    - (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时它是空的,但当我尝试加载数据时,我仍然无法加载数据

显示持久存储区为空的调试

代码语言:javascript
复制
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的代码

代码语言:javascript
复制
- (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];
}
EN

回答 1

Stack Overflow用户

发布于 2016-11-23 18:29:37

问题是我并没有在所有地方都一致地使用appDelegate

WebServices类中的以下代码行需要出现在所有位置

代码语言:javascript
复制
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];

感谢Vinodh和Flexicoder指出这一点

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40758392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档