为什么iOS通过UIManagedDocument保存Core Data时会延迟?

And*_*rew 2 sqlite core-data objective-c ios

我正在使用Core Data在SQLite数据库中保留一些信息.我正在使用模拟器进行测试,并使用sqlite3查询数据库并验证我正在存储我期望的内容.

我所看到的是,数据在我保存之后的15-20秒内才出现在SQLite数据库中.

这是我将我的保存减少到的代码:

NSEntityDescription *customerType = [NSEntityDescription entityForName:@"CustomerType" inManagedObjectContext:context];
CustomerType *ct = [[CustomerType alloc]initWithEntity:customerType insertIntoManagedObjectContext:context ];
ct.code = code;

NSError* error = nil;
if (![context save:&error] || error)
    NSLog(@"Saved new customer (error=%@)", [error debugDescription]);
Run Code Online (Sandbox Code Playgroud)

保存操作完成且没有错误.

关于为什么我看到这种延迟的任何暗示?我的保存操作是否未正常运行,或者是否将数据持久化为模拟器的工件?

Shm*_*idt 5

因为你应该使用:

    [self.document saveToURL:self.document.fileURL
            forSaveOperation:UIDocumentSaveForOverwriting
           completionHandler:^(BOOL success) {                   
               if (success) {                       
                   NSLog(@"saved");
               } else {                       
                   NSLog(@"unable to save");
               }
               }];
Run Code Online (Sandbox Code Playgroud)