如果 iCloud 开启,应用程序崩溃(链接到与 coreData 同步)

Posted

技术标签:

【中文标题】如果 iCloud 开启,应用程序崩溃(链接到与 coreData 同步)【英文标题】:App Crash if iCloud is on (linked to sync with coreData) 【发布时间】:2013-02-25 11:40:35 【问题描述】:

这是我在 init 中用于存储类的代码,直到最近它都运行良好。当我尝试测试它时,它在 addPersistentStoreWithType 处崩溃(标记如下)......我在切换到 iPhone 5 后注意到了这一点正在尝试运行该应用程序...但是如果我在手机上关闭 icloud 或在模拟器上对其进行测试,则没有问题...

-(id)init

    self = [super init];
    if(self)
    
        NSLog(@"%s", __FUNCTION__);
        favColors = [[NSMutableArray alloc] init];
        model = [NSManagedObjectModel mergedModelFromBundles:nil];
        NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
        NSString *path = [self itemArchivePath];
        NSURL *storeURL = [NSURL fileURLWithPath:path];
        NSError *error = nil;
        NSMutableDictionary *options = [NSMutableDictionary dictionary];

        NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
        if (ubiq) 
            NSLog(@"iCloud access at %@", ubiq);
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(contentChange:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:nil];
            NSFileManager *fm = [NSFileManager defaultManager];
            NSURL *ubcontainer = [fm URLForUbiquityContainerIdentifier:nil];
            [options setObject:@"color" forKey:NSPersistentStoreUbiquitousContentNameKey];
            [options setObject:ubcontainer forKey:NSPersistentStoreUbiquitousContentURLKey];

         else 
            NSLog(@"No iCloud access");
        
   // ************ Crash here **************
        if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:(ubiq?options:nil) error:&error]) 
            [NSException raise:@"Open failed" format:@"Reason: %@", [error localizedDescription]];
        
        context = [[NSManagedObjectContext alloc] init];
        [context setPersistentStoreCoordinator:psc];
        [context setUndoManager:nil];

        [self loadAllItems];
    
    return self;


- (NSString *)itemArchivePath

    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [documentDirectories objectAtIndex:0];
    return [documentDirectory stringByAppendingPathComponent:@"store.data"];

另外,如果我运行应用程序一次并在崩溃后关闭 icloud 并再次运行应用程序.. 数据将从 icloud 中检索...并显示在应用程序上。

输出日志显示了这一点。

iCloud 访问文件://localhost/private/var/mobile/Library/Mobile%20Documents/xxx -PFUbiquitySetupAssistant performPreStoreSetupWithError:: CoreData: Ubiquity: 基线文件存在,但无法读取

【问题讨论】:

在我进入 iCloud>Storage and Backup>Manage Storage 并删除了应用程序的数据后,它工作正常......以前的数据又回来了......:/ 【参考方案1】:

这是一个内部 iCloud 错误。这不是你的错,你无法在代码中做任何事情来修复或阻止它。它会随机发生,没有可预测的模式何时或为什么。不幸的是,删除应用程序的数据是唯一真正的解决方案。这样做只会删除 iCloud 的数据副本,而不是存储在应用程序的 Documents 目录或其他非 iCloud 位置中的数据,因此旧数据仍然存在也就不足为奇了。

【讨论】:

所以我应该在应用程序描述中为遇到此问题的人写下这个吗?我什至不知道他们是否有这个问题。我做到了,我在我的朋友的电话,它也发生了。 哦,它已经在现场了?那真不幸。您的用户可能会在某个时候遇到问题,但可能不会立即出现。您可能首先点击它,因为由于您正在开发应用程序,因此您会更多地使用它。你可以把它放在应用描述中,或者放在你的网站上,但你应该试着以某种方式把它弄出来。 酷,thnx.. 真的希望有一个修复它.. din 想把它放在描述中。 难道没有类似 try catch 之类的东西我可以把它放进去解决错误吗? 如果应用程序崩溃,使用 try/catch 可以帮助您避免崩溃,但它不会帮助您让 iCloud 正常工作。该应用程序可以继续运行,但无法访问其数据或同步该数据。

以上是关于如果 iCloud 开启,应用程序崩溃(链接到与 coreData 同步)的主要内容,如果未能解决你的问题,请参考以下文章

iCloud 打开/关闭错误

切换 icloud 帐户时创建持久存储时应用程序崩溃

iCloud保存数据与UIDocument崩溃

iCloud 状态更改时我的应用程序崩溃

-initWithDocumentTypes iCloud 仅在生产版本中崩溃

当我从 iCloud 中选择照片时,应用程序崩溃了