EXC_BAD_ACCESS using iCloud on multiple devices

I am creating an application with iCloud. But I have some problems. It creates a directory in iCloud using NSFileWrapper, then creates an NSData file (container) in the NSFileWrapper directory. I use this code to convert NSFileWrapper to NSMutableArray:

NSFileWrapper *MyWrapper=[[[MyDocument data] fileWrappers] objectForKey:@"myFile.doh"];    NSData *MyData=[NSData dataWithData:[MyWrapper regularFileContents]];    NSMutableArray *MyList=[NSPropertyListSerialization propertyListFromData:MyData mutabilityOption:NSPropertyListMutableContainers format:nil errorDescription:nil]; 

And it only works correctly on the device that created this container. On other devices, the result of this code is BAD_ACCESS (in the second line of code, where I start to do something with the data). During debugging, the "regularFileContents" function returns the correct object with the correct data size, but when I try to read this data, BAD_ACEESS (code = 10) occurs.

I use ARC, so this is not a memory management error.

Maybe the problem is in some project code / code settings? Any ideas?

Thanks!

+4
source share
1 answer

I ran into this, and after a lot of experimentation, I found that although the outer shell loaded, the inner content had not yet loaded, and this caused the regularFileContents call to fail.

I called startDownloadingUbiquitousItemAtURL on MyWrapper, and as soon as this completes, the error went away. Here is a method that checks the downloaded file status (assuming you know the URL of your MyWrapper) and starts the download if it is not already loaded.

 -(BOOL)downloadFileIfNotAvailable:(NSURL*)fileURL { NSNumber *isInCloud = nil; if ([fileURL getResourceValue:&isInCloud forKey:NSURLIsUbiquitousItemKey error:nil]) { if ([isInCloud boolValue]) { NSNumber *isDownloaded = nil; if ([fileURL getResourceValue:&isDownloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:nil]) { if ([isDownloaded boolValue]) { return YES; } NSError *error = nil; [[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:fileURL error:&error]; if (error) { NSLog(@"Download Failed :: %@", error); } return NO; } } } return YES; } 
+1
source

Source: https://habr.com/ru/post/1414225/


All Articles