【问题标题】:Black screen when attempting to start iCloud app on phone尝试在手机上启动 iCloud 应用程序时出现黑屏
【发布时间】:2013-06-03 19:43:05
【问题描述】:

我现在可以根据我是否在模拟器中触发 iCloud 加载。当我尝试在真实设备上运行时,出现黑屏并且“addPersistentStore”行似乎挂起。 “我的项目名称”是权利文件的名称,也是应用程序的名称。

发生了什么事?

#if (TARGET_IPHONE_SIMULATOR)
        if (![psc addPersistentStoreWithType:NSSQLiteStoreType
                               configuration:nil
                                         URL:dbUrl
                                     options:nil
                                       error:&error]) {
            [NSException raise:@"Open failed" format:@"Reason: %@", [error localizedDescription]];
        }
#else
        NSFileManager *fm = [NSFileManager defaultManager];
        NSURL *ubContainer = [fm URLForUbiquityContainerIdentifier:nil];
        NSMutableDictionary *options = [NSMutableDictionary dictionary];
        [options setObject:@"My Project Name" forKey:NSPersistentStoreUbiquitousContentNameKey];
        [options setObject:ubContainer forKey:NSPersistentStoreUbiquitousContentURLKey];

        if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:dbUrl options:options error:&error]) {
            [NSException raise:@"Open failed" format:@"%@", [error localizedDescription]];
        }
#endif

【问题讨论】:

    标签: ios objective-c icloud


    【解决方案1】:

    Apple 建议您在使用 iCloud 时,应在单独的线程上执行所有这些步骤。 URLForUbiquityContainerIdentifieraddPersistentStoreWithType:configuration:options:error: 都将连接到网络,并且可能会长时间阻塞。第二个调用——添加持久存储——可能会阻塞更长的时间。在 iOS 上,iCloud 数据仅按需下载,并且当您添加持久存储时会发生这种需求。你得到一个空白屏幕,因为NSPersistentStoreCoordinator 正忙于与网络交谈(或试图这样做)。 Apple 的示例代码将此放在一个单独的队列中,您也应该这样做。

    【讨论】:

      【解决方案2】:

      您的代码没有表明这一点,但您不能在主线程上调用-URLForUbiquityContainerIdentifier。来自 Apple documentation 的注意事项:

      重要提示:请勿从应用的主线程调用此方法。 因为此方法可能需要大量时间来设置 iCloud 并返回请求的 URL,您应该始终从 次要线程。确定 iCloud 是否可用,尤其是在 启动时,请改为调用 ubiquityIdentityToken 方法。

      这很可能需要很长时间,并且看起来好像您的应用没有加载,而实际上它只是在等待该方法返回。

      【讨论】:

      • 没有。那条线返回就好了。挂起的是 addPersistentStore。
      • 我仍然会尝试在不同的线程上运行它。在这个 SO 问题中,有人遇到了同样的问题:stackoverflow.com/questions/12923238/…
      • 与我的问题无关的好建议。如果没有 iCloud工作,如果有错误或性能不佳,我会给 0 大便。
      • addPersistentStoreWithType:configuration:URL:options:error: 添加到不同的线程也没有帮助?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      相关资源
      最近更新 更多