设置项无处不在的奇怪行为

时间:2013-12-01 18:35:14

标签: objective-c core-data icloud uimanageddocument

问题是,我在某个本地目录中有一个UIManagedDocument。我无处不在。 这是代码:

-(void)queryGatheredInfo:(NSNotification*)notification{
    NSLog(@"Query gathered info!");
    [[NSNotificationCenter defaultCenter]removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:self.documentQuery];
    NSLog(@"Removed AppDelegate as an observer for NSMetadataQueryDidFinishGatheringNotification.");
    [self.documentQuery disableUpdates];
    NSMetadataQuery* tempQuery=[notification object];
    if(tempQuery.resultCount){
        NSLog(@"Query Found Items:");
        for (int i=0;i<tempQuery.resultCount;i++){
            NSMetadataItem* item=[tempQuery resultAtIndex:i];
            NSURL* itemURL=[item valueForAttribute:NSMetadataItemURLKey];
            NSString* itemFSName=[[self filePackageURLForCloudURL:itemURL]lastPathComponent];
            NSLog(@"Item %d: %@",i,itemFSName);
            if([itemFSName isEqualToString:kUSER_DOCUMENT_NAME]){
                NSLog(@"FOUND A SAVED DOCUMENT IN iCLOUD!");
                [self loadDocumentFromiCloudAtURL:[itemURL URLByDeletingLastPathComponent]withPersistentStoreOptions:[self getPersistentOptionsFromDocumentMetadataAtURL:itemURL]];
                break;
            }
        }
    }else{
        NSLog(@"Query did not found a document in iCloud!");
        if([[NSFileManager defaultManager]fileExistsAtPath:[[self localDocumentURL]path]]){
            NSLog(@"Document was found in local documents directory.");
            [self transferFileFromLocalDirectory:[self localDocumentURL] ToiCloudDirectory:[[self iCloudDocumentsURL]URLByAppendingPathComponent:kUSER_DOCUMENT_NAME]];
        }else{
            NSLog(@"Creating new document in iCloud!");
            [self createNewDocumentIniCloud];
        }
    }
    [self.documentQuery stopQuery];
}
-(void)transferFileFromLocalDirectory:(NSURL*)localURL ToiCloudDirectory:(NSURL*)iCloudURL{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSError* error=nil;
        BOOL flag=[[NSFileManager defaultManager]setUbiquitous:YES itemAtURL:localURL destinationURL:iCloudURL error:&error];
        if(flag){
            NSLog(@"Successfully set document ubiquitous");
            UIManagedDocument* doc=[[UIManagedDocument alloc]initWithFileURL:iCloudURL];
            doc.persistentStoreOptions=[self getPersistentOptionsFromDocumentMetadataAtURL:[iCloudURL URLByAppendingPathComponent:@"DocumentMetadata.plist"]];
            [self openDocument:doc];
        }else{
            NSLog(@"There was an error setting document Ubiquitous.\n%@",error);
        }
    });
}

在此之后,它开始下载自己的副本2次。所以最后我在我的核心数据中获得了3个相同的项目。但是,如果我删除应用程序,并再次运行它,它会在icloud中找到文档,加载它,它看起来没问题,没有重复的项目。 我的问题是:当我将文档无处不在时,会发生什么?为什么加载重复?

当项目无处不在时,这是输出:

2013-12-01 22:02:56.452 iGymPRO[5517:60b] iCloud is available
2013-12-01 22:02:56.456 iGymPRO[5517:60b] Added AppDelegate as an observer for NSMetadataQueryDidFinishGatheringNotification.
2013-12-01 22:02:56.458 iGymPRO[5517:60b] Query started!
2013-12-01 22:02:56.500 iGymPRO[5517:60b] Query gathered info!
2013-12-01 22:02:56.502 iGymPRO[5517:60b] Removed AppDelegate as an observer for NSMetadataQueryDidFinishGatheringNotification.
2013-12-01 22:02:56.503 iGymPRO[5517:60b] Query did not found a document in iCloud!
2013-12-01 22:02:56.569 iGymPRO[5517:60b] Document was found in local documents directory.
2013-12-01 22:02:56.609 iGymPRO[5517:1803] Successfully set document ubiquitous
2013-12-01 22:02:56.983 iGymPRO[5517:3e03] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](754): CoreData: Ubiquity:  mobile~5815F60F-67BF-4FD1-8376-B509672B469D:UserDataDocumentContent
Using local storage: 1
2013-12-01 22:02:57.055 iGymPRO[5517:60b] Opened document
2013-12-01 22:02:57.057 iGymPRO[5517:60b] Removing delegate as an observer for UIDocumentStateChangedNotification
2013-12-01 22:02:57.059 iGymPRO[5517:60b] Listening to UIDocumentStateChangedNotification
2013-12-01 22:02:57.060 iGymPRO[5517:60b] Managed Object Context is ready to use! Dismissing loading screen
2013-12-01 22:02:57.925 iGymPRO[5517:4707] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](754): CoreData: Ubiquity:  mobile~5815F60F-67BF-4FD1-8376-B509672B469D:UserDataDocumentContent
Using local storage: 0
2013-12-01 22:03:02.650 iGymPRO[5517:4707] -[PFUbiquitySetupAssistant finishSetupWithRetry:](811): CoreData: Ubiquity:  <PFUbiquitySetupAssistant: 0x1463c2f0>: Retrying after delay: 60
2013-12-01 22:04:09.390 iGymPRO[5517:470b] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](754): CoreData: Ubiquity:  mobile~5815F60F-67BF-4FD1-8376-B509672B469D:UserDataDocumentContent
Using local storage: 0
2013-12-01 22:04:13.875 iGymPRO[5517:470b] -[PFUbiquitySetupAssistant finishSetupWithRetry:](811): CoreData: Ubiquity:  <PFUbiquitySetupAssistant: 0x1463c2f0>: Retrying after delay: 120
2013-12-01 22:06:59.170 iGymPRO[5517:4717] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](754): CoreData: Ubiquity:  mobile~5815F60F-67BF-4FD1-8376-B509672B469D:UserDataDocumentContent
Using local storage: 0
2013-12-01 22:13:34.370 iGymPRO[5517:4227] __45-[PFUbiquityFilePresenter processPendingURLs]_block_invoke(305): CoreData: Ubiquity:  Librian returned a serious error for starting downloads Error Domain=LibrarianErrorDomain Code=1 "The operation couldn’t be completed. (LibrarianErrorDomain error 1 - Unable to initiate download.)" UserInfo=0x1467b370 {Item Errors={
    "file:///private/var/mobile/Library/Mobile%20Documents/476DPCGZ25~trenbolone~SP~iGym/CoreData/UserDataDocumentContent/mobile~5815F60F-67BF-4FD1-8376-B509672B469D/UserDataDocumentContent/GxrbFeXHGcfBNM0uvaCzuqHE4XmcYCiCKbgWv7OTybM=/receipt.0.cdt" = "Error Domain=UBErrorDomain Code=10 \"The operation couldn\U2019t be completed. (UBErrorDomain error 10 - Error Domain=UBErrorDomain Code=10 \"The operation couldn\U2019t be completed. (UBErrorDomain error 10 - The item can't be found on disk, is not yet scanned or was deleted)\" UserInfo=0x14570a30 {NSDescription=The item can't be found on disk, is not yet scanned or was deleted})\" UserInfo=0x1463c6a0 {NSDescription=Error Domain=UBErrorDomain Code=10 \"The operation couldn\U2019t be completed. (UBErrorDomain error 10 - The item can't be found on disk, is not yet scanned or was deleted)\" UserInfo=0x14570a30 {NSDescription=The item can't be found on disk, is not yet scanned or was deleted}}";
}, NSUnderlyingError=0x1470d510 "The operation couldn’t be completed. (UBErrorDomain error 10 - The item can't be found on disk, is not yet scanned or was deleted)", NSDescription=Unable to initiate download.} with userInfo {
    "Item Errors" =     {
        "file:///private/var/mobile/Library/Mobile%20Documents/476DPCGZ25~trenbolone~SP~iGym/CoreData/UserDataDocumentContent/mobile~5815F60F-67BF-4FD1-8376-B509672B469D/UserDataDocumentContent/GxrbFeXHGcfBNM0uvaCzuqHE4XmcYCiCKbgWv7OTybM=/receipt.0.cdt" = "Error Domain=UBErrorDomain Code=10 \"The operation couldn\U2019t be completed. (UBErrorDomain error 10 - Error Domain=UBErrorDomain Code=10 \"The operation couldn\U2019t be completed. (UBErrorDomain error 10 - The item can't be found on disk, is not yet scanned or was deleted)\" UserInfo=0x14570a30 {NSDescription=The item can't be found on disk, is not yet scanned or was deleted})\" UserInfo=0x1463c6a0 {NSDescription=Error Domain=UBErrorDomain Code=10 \"The operation couldn\U2019t be completed. (UBErrorDomain error 10 - The item can't be found on disk, is not yet scanned or was deleted)\" UserInfo=0x14570a30 {NSDescription=The item can't be found on disk, is not yet scanned or was deleted}}";
    };
    NSDescription = "Unable to initiate download.";
    NSUnderlyingError = "Error Domain=UBErrorDomain Code=10 \"The operation couldn\U2019t be completed. (UBErrorDomain error 10 - The item can't be found on disk, is not yet scanned or was deleted)\" UserInfo=0x145d6340 {NSDescription=The item can't be found on disk, is not yet scanned or was deleted}";
}

当我删除并运行我的应用程序时(当一切都正常加载时)输出

2013-12-01 22:32:49.748 iGymPRO[5525:60b] iCloud is available
2013-12-01 22:32:49.754 iGymPRO[5525:60b] Added AppDelegate as an observer for NSMetadataQueryDidFinishGatheringNotification.
2013-12-01 22:32:49.756 iGymPRO[5525:60b] Query started!
2013-12-01 22:32:49.897 iGymPRO[5525:60b] Query gathered info!
2013-12-01 22:32:49.898 iGymPRO[5525:60b] Removed AppDelegate as an observer for NSMetadataQueryDidFinishGatheringNotification.
2013-12-01 22:32:49.899 iGymPRO[5525:60b] Query Found Items:
2013-12-01 22:32:49.959 iGymPRO[5525:60b] Item 0: Data Document
2013-12-01 22:32:49.960 iGymPRO[5525:60b] FOUND A SAVED DOCUMENT IN iCLOUD!
2013-12-01 22:32:50.031 iGymPRO[5525:60b] Opening document from iCloud
2013-12-01 22:32:54.203 iGymPRO[5525:5003] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](754): CoreData: Ubiquity:  mobile~5815F60F-67BF-4FD1-8376-B509672B469D:UserDataDocumentContent
Using local storage: 1
2013-12-01 22:32:54.259 iGymPRO[5525:60b] Opened document
2013-12-01 22:32:54.260 iGymPRO[5525:60b] Removing delegate as an observer for UIDocumentStateChangedNotification
2013-12-01 22:32:54.261 iGymPRO[5525:60b] Listening to UIDocumentStateChangedNotification
2013-12-01 22:32:54.262 iGymPRO[5525:60b] Managed Object Context is ready to use! Dismissing loading screen
2013-12-01 22:33:57.635 iGymPRO[5525:1803] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](754): CoreData: Ubiquity:  mobile~5815F60F-67BF-4FD1-8376-B509672B469D:UserDataDocumentContent
Using local storage: 0

1 个答案:

答案 0 :(得分:0)

很高兴对此进行更正,但我认为这不是通过iCloud将UIManagedDocument设置为同步的正确方法。如果您在本地目录中有UIManagedDocument,则需要使用persistentStoreCoordinators migratePersistentStore方法将存储本身迁移到iCloud,以便在iCloud中重新创建日志文件。

当您删除并重新安装应用程序时,我感到非常惊讶。