2013-08-16 28 views
17

我正在处理使用不同版本的coredata处理迁移的iOS项目。persistentstorecoordinator sqlite错误代码:522'没有错误'

我也试过在一个catch周围的if语句,它返回一个sqlite的错误代码522

这有什么错吗?

我下面的代码:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator{ 
    if (__persistentStoreCoordinator != nil) { 
    return __persistentStoreCoordinator; 
    } 
    NSURL *storeURL = [[self applicationDocumentsDirectory]  
    URLByAppendingPathComponent:@"coredatadb.sqlite"]; 
    NSError *error = nil; 
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
    [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
    [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]){ 

     [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]; 
     [__persistentStoreCoordinator release]; 
     __persistentStoreCoordinator = nil; 
     return [self persistentStoreCoordinator]; 
    } 

    return __persistentStoreCoordinator; 

回答

3

因此,它看起来像下面的解决我的具体问题,虽然有些人会建议不要更改日志模式上的SQLite数据库:

// Change journal mode from WAL to MEMORY 
NSDictionary *pragmaOptions = [NSDictionary dictionaryWithObject:@"MEMORY" forKey:@"journal_mode"]; 

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
pragmaOptions, NSSQLitePragmasOption, nil]; 
+0

Thnaks for your input。加上Kevinl的观点,我们可以使用“DELETE”而不是“MEMORY”https://www.sqlite.org/pragma.html#pragma_journal_mode – Boobalan

38

通过改变日记模式,你只能避免这个问题,但你没有修复它。新的日志模式为您的应用程序提供了诸如速度等一些优势。

真正的问题是,您只删除1个文件,而不是在该模式下使用的所有3个文件。不仅有coredatadb.sqlite,还有coredatadb.sqlite-shm和coredatadb.sqlite-wal文件。有关模式和预先写入的内容,请查看核心数据上的WWDC 2013视频以了解详细信息。

解决您的问题,您应该删除文件目录下,以“coredatadb.sqlite”开头的所有文件,一切都是免费的,又容易;-)

更新为iOS 9:更容易,更安全现在使用destroyPersistentStoreAtURL或replacePersistentStoreAtURL。请参阅WWDC 2015会话220_hd_whats_new_in_core_data。

+0

这是正确的答案。使用这个其他的答案来删除匹配特定模式的文件:http://stackoverflow.com/questions/6179667/remove-files-matching-a-pattern-in-ios –