Настройка элемента вездесущего странного поведения
Дело в том, что у меня есть 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 ответ
Рад, что это поправили, но я не думаю, что это правильный способ настроить UIManagedDocument для синхронизации через iCloud. Если у вас есть UIManagedDocument в локальном каталоге, вам нужно перенести само хранилище в iCloud с помощью persistentStoreCoordinators migratePersistentStore
метод для того, чтобы воссоздать файлы журнала в iCloud.
Я очень удивлен, что это работает вообще, когда вы удаляете и переустанавливаете приложение.