有谁知道这里发生了什么?核心数据/ iCloud

时间:2013-10-08 20:50:16

标签: ios objective-c core-data icloud

所以这里发生的事情是应用程序刚刚收到NSPersistentStoreDidImportUbiquitousContentChangesNotification,我刚刚调用了

- (void)storesDidUpdate:(NSNotification*)note {
    FLOG(@"storesDidUpdate ");
    [_managedObjectContext mergeChangesFromContextDidSaveNotification:note];

    // Refresh user Interface
    [[NSNotificationCenter defaultCenter] postNotificationName:@"iProjectCoreDataUpdated"
                                                        object:self];

    [self updateDetails];
}

现在该应用程序正在尝试使用新的文本副本更新UITextView,只需执行以下操作:

self.detailText.attributedText  = [self.detailItem valueForKey:@"scope"];

其中detailItem是在从iCloud接收更新之前检索到的NSManagedObject。

不确定textContainer为什么抱怨或抱怨什么,也不确定为什么导入日志正在发生,因为我已经收到通知表明已完成!

奇怪的是,如果我只是调用reloadData,其他UITableViews会正确更新。

关于我是否在这里做错事的任何想法?请注意,如果我不尝试更新textView它工作正常,如果我关闭视图并返回到它然后我得到正确的数据。

此外,当我重新启动应用程序时,所有数据都存在,没有任何损坏或任何内容,事实上,当涉及到Core Data存储时,应用程序在大多数方面看起来非常强大,尽管iCloud的任何一方都出现了问题! / p>

哦,reloadFetchedResults有点误导,因为我似乎不需要这样做,因此方法名称是过去的宿醉,我正在做的就是在此调用的UI中刷新值。

  

2013-10-09 07:25:53.783 MyApp [4509:510b] OpeningViewController.reloadFetchedResults:called   2013-10-09 07:25:53.786 MyApp [4509:510b] InfoDetailViewController.reloadFetchedResults:在InfoDetailViewController中调用   2013-10-09 07:25:53.788 MyApp [4509:510b] *断言失败_UIPerformResizeOfTextViewForTextContainer(NSLayoutManager *,UIView *,NSTextContainer *,NSUInteger)(),/ SourceCache / UIFoundation / UIFoundation-258 / UIFoundation /TextSystem/NSLayoutManager_Private.m:1510   2013-10-09 07:25:53.793 MyApp [4509:510b] -_PFUbiquityRecordImportOperation main:CoreData:Ubiquity:导入事务日志时出错:       transactionLogLocation :: / var / mobile / Library / Mobile Documents / HHHHHHNNNN~com~mycompany~MyApp / CoreData / New Document / duncangroenewald~sIMAABC628E-9D5E-58F7-9B8D-0BC724C6D0C8 / New Document / W8MckEJ0x2d~HZZIeUH2be6hs41TEOONzKIrCuLcuP4 = / 6C953E7C-B2AF- 47F7-B828-DD062B96415D.1.cdt       transactionNumber:5   ,例外:仅在主线程上运行!    用户信息:(null)   2013-10-09 07:25:53.803 MyApp [4509:510b] -_PFUbiquityRecordsImporter operation:failedWithError :: CoreData:Ubiquity:导入操作遇到错误:Error Domain = NSCocoaErrorDomain Code = 134060“操作无法完成。( Cocoa错误134060.)“UserInfo = 0x16d882c0 {exception =仅在主线程上运行!}   用户信息: {       exception =“只在主线程上运行!”;   }。尝试在URL中导入日志文件时:       transactionLogLocation :: / var / mobile / Library / Mobile Documents / HHHHHHNNNN~com~mycompany~MyApp / CoreData / New Document / duncangroenewald~sIMAABC628E-9D5E-58F7-9B8D-0BC724C6D0C8 / New Document / W8MckEJ0x2d~HZZIeUH2be6hs41TEOONzKIrCuLcuP4 = / 6C953E7C-B2AF- 47F7-B828-DD062B96415D.1.cdt       transactionNumber:5   2013-10-09 07:25:53.809 MyApp [4509:510b] * 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因是:'只在主线程上运行!'   ***第一次抛出调用堆栈:   (0x2ff23f53 0x3a6996af 0x2ff23e2d 0x308cb1df 0x3796643d 0x37966185 0x3798f7bb 0x379926f7 0x37992759 0x379b378b 0x379b331f 0x379b2f0d 0x3273b05d 0x129717 0x2fee6121 0x2fe5a317 0x3083edcd 0x308436ab 0x118263 0x2fee6121 0x2fe5a317 0x2fd8c69f 0x2fd8cc93 0x2fd813dd 0x3085197b 0x308f5b35 0x3ab83297 0x3ab8309b 0x3ab83d15 0x3ab83f8d 0x3acbedbf 0x3acbec84)   libc ++ abi.dylib:以NSException类型的未捕获异常终止

编辑: 我在这里发布了一些针对iOS和OSX的示例Core Data / iCloud应用程序 - 它们包括用于加载/删除数据以及iCloud同步的后台线程。希望能解决这里描述的问题。 http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/

4 个答案:

答案 0 :(得分:9)

好的,这似乎是解决它的问题 - 我想我从未意识到接收通知会在另一个线程上运行。这是正常行为,您收到的所有核心数据通知都是在后台线程上调用的吗?如果是这样,你总是要确保你所做的一切都是在正确的线程上运行吗?

- (void)storesDidUpdate:(NSNotification*)note {
    FLOG(@"storesDidUpdate ");
    [_managedObjectContext mergeChangesFromContextDidSaveNotification:note];

    [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
        //Your code goes in here
        LOG(@" Main Thread Code");

        // Refresh user Interface
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CoreDataUpdatedNotification"
                                                        object:self];
    }];

}

编辑:

我终于开始记录如何让Core Data和iCloud非常可靠地工作,包括处理iCloud的移动以及响应使用更改其Use iCloud首选项设置的情况。当我试图弄清楚如何使其工作时,我无法找到工作代码的良好解释,所以我希望这证明对其他人有用。其中包括应用程序的视频,您可以看到它的行为,以帮助您弄清楚它是否是您想要实现的。

http://ossh.com.au/design-and-technology/software-development/

答案 1 :(得分:3)

我遇到了同样的问题。这正是@John Rogers解释的问题。 我创建了UITextViews,并在后台调用了一个URL来获取一些数据,这些数据应该插入到我生成的UITextView中。但问题是方法sendAsynchronousRequest将在后台调用并导致我的崩溃。

我可以通过在接收数据时在主线程上执行特定选择器来解决问题。所以也许你可以将方法调用插入到另一个方法中并以这种方式调用它:

[self performSelectorOnMainThread:@selector(received:) 
                       withObject:data 
                    waitUntilDone:YES];

这样你就可以再次回到主线程。

答案 2 :(得分:0)

是的,我明白了,但我没有在任何线程上做任何事情,所以其他必须是。我正在更新UITextView,我猜它可能会导致失败。我不清楚为什么这与ubiquityImporter有任何关系 - 也许能够解释下面这些内容的人可以指出我正确的方向。我是以某种方式在错误的线程上运行的东西?

First throw call stack:
(
    0   CoreFoundation                      0x027c55e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x019238b6 objc_exception_throw + 44
    2   CoreFoundation                      0x027c5448 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x01062960 -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 101
    4   UIFoundation                        0x01b33131 -[NSLayoutManager(NSPrivate) _resizeTextViewForTextContainer:] + 419
    5   UIFoundation                        0x01b32e16 -[NSLayoutManager(NSPrivate) _recalculateUsageForTextContainerAtIndex:] + 2083
    6   UIFoundation                        0x01b67675 _enableTextViewResizing + 234
    7   UIFoundation                        0x01b6b275 -[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] + 688
    8   UIFoundation                        0x01b6b2fa -[NSLayoutManager processEditingForTextStorage:edited:range:changeInLength:invalidatedRange:] + 82
    9   UIFoundation                        0x01b93d35 -[NSTextStorage _notifyEdited:range:changeInLength:invalidatedRange:] + 153
    10  UIFoundation                        0x01b93870 -[NSTextStorage processEditing] + 462
    11  UIFoundation                        0x01b93419 -[NSTextStorage endEditing] + 80
    12  UIFoundation                        0x01b934a3 -[NSTextStorage coordinateEditing:] + 66
    13  UIKit                               0x007f4f48 -[UITextView setAttributedText:] + 254
    14  MyApp                        0x00027d7a -[WBSDetailViewController displayItem] + 3146
    15  MyApp                        0x00028c6c -[WBSDetailViewController refreshUI:] + 156
    16  Foundation                          0x01088e39 __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke + 40
    17  CoreFoundation                      0x02821524 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
    18  CoreFoundation                      0x0277907b _CFXNotificationPost + 2859
    19  Foundation                          0x00fc2b91 -[NSNotificationCenter postNotificationName:object:userInfo:] + 98
    20  Foundation                          0x00fd206a -[NSNotificationCenter postNotificationName:object:] + 55
    21  MyApp                        0x00031095 -[BaseAppDelegate storesDidUpdate:] + 293
    22  Foundation                          0x01088e39 __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke + 40
    23  CoreFoundation                      0x02821524 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
    24  CoreFoundation                      0x0277907b _CFXNotificationPost + 2859
    25  Foundation                          0x00fddd7b -[NSNotificationCenter postNotification:] + 121
    26  CoreData                            0x0173c5f4 -[_PFUbiquityRecordsImporter postImportNotificationForStoreName:andLocalPeerID:withUserInfo:] + 1892
    27  CoreData                            0x0173cbc2 -[_PFUbiquityRecordsImporter operationDidFinish:] + 706
    28  CoreData                            0x0172e8ce -[_PFUbiquityRecordImportOperation main] + 15102
    29  Foundation                          0x0108aa69 -[__NSOperationInternal _start:] + 671
    30  Foundation                          0x01007798 -[NSOperation start] + 83
    31  Foundation                          0x0108cd34 __NSOQSchedule_f + 62
    32  libdispatch.dylib                   0x05b794b0 _dispatch_client_callout + 14
    33  libdispatch.dylib                   0x05b67088 _dispatch_queue_drain + 450
    34  libdispatch.dylib                   0x05b66e85 _dispatch_queue_invoke + 126
    35  libdispatch.dylib                   0x05b67e25 _dispatch_root_queue_drain + 83
    36  libdispatch.dylib                   0x05b6813d _dispatch_worker_thread2 + 39
    37  libsystem_pthread.dylib             0x05f05dab _pthread_wqthread + 336
    38  libsystem_pthread.dylib             0x05f09cce start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException

答案 3 :(得分:0)

阅读所有答案+评论(和你的修复) - 我认为应该注意你必须只从主线程更新任何UI控件。

我假设[self updateDetails];调用了self.detailText.attributedText = [self.detailItem valueForKey:@"scope"];代码?...这是更新UI的实际代码?

假设您可以使用-performSelectorOnMainThread执行对-updateDetails的调用:或者在-updateDetails中检查您是否在主线程([NSThread isMainThread])上执行,并将UI更新代码包装在那里以便调用者他们不关心他们称之为-updateDetails的线程。