I have an issue where I can't seem to dealloc UIDocument (used in iCloud)
After running an NSMetaDataQuery to look for the document as follows..
NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
_query = query;
[query setSearchScopes:[NSArray arrayWithObject:
NSMetadataQueryUbiquitousDocumentsScope]];
NSPredicate *pred = [NSPredicate predicateWithFormat:
@"%K == %@", NSMetadataItemFSNameKey, kFILENAME];
[query setPredicate:pred];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(queryDidFinishGathering:)
name:NSMetadataQueryDidFinishGatheringNotification
object:query];
[query startQuery];
I process my query
- (void)queryDidFinishGathering:(NSNotification *)notification {
NSMetadataQuery *query = [notification object];
[query disableUpdates];
[query stopQuery];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSMetadataQueryDidFinishGatheringNotification
object:query];
_query = nil;
[self loadData:query];
}
Then load or create a new document.
- (void)loadData:(NSMetadataQuery *)query {
if ([query resultCount] == 1) {
NSMetadataItem *item = [query resultAtIndex:0];
NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
MyDocument *doc = [[[MyDocument alloc] initWithFileURL:url] autorelease];
[doc openWithCompletionHandler:^(BOOL success) {
if (success) {
NSLog(@"iCloud document opened %@", doc);
[doc updateChangeCount:UIDocumentChangeDone];
} else {
NSLog(@"failed opening document from iCloud");
}
}];
} else {
NSURL *ubiq = [[NSFileManager defaultManager]
URLForUbiquityContainerIdentifier:nil];
NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:
@"Documents"] URLByAppendingPathComponent:kFILENAME];
MyDocument *doc = [[[MyDocument alloc] initWithFileURL:ubiquitousPackage] autorelease];
[doc saveToURL:[doc fileURL]
forSaveOperation:UIDocumentSaveForCreating
completionHandler:^(BOOL success) {
if (success) {
[doc openWithCompletionHandler:^(BOOL success) {
NSLog(@"new document opened from iCloud");
[doc updateChangeCount:UIDocumentChangeDone];
}];
}
}];
}
}
The NSLog(@"iCloud document opened %@", doc); shows a different memory address for each UIDocument.
I have an NSLog in my UIDocument subclass, it never gets called. I cannot see where it is being retained that I am not releasing it. This query is ran whenever I want to sync my cloud data, this happens fairly regularly. The data syncs correctly.
I am experiencing strange crashes where my app will simply close to the dashboard, with nothing in the debug (from previous experiences I know this often to be the app expending too much memory and being terminated.)
I think that my UIDocument is leaking, would I be correct in this assumption, this is the first time i've wrestled with iCloud so I'm still in the dark over a few things.
My subclass has the following properties:
@property (copy, nonatomic) NSData *infoData;
@property (copy, nonatomic) NSMutableArray *firstArray;
@property (copy, nonatomic) NSMutableArray *secondArray;
@property (copy, nonatomic) NSMutableArray *thirdArray;
I am not using ARC.
I did not realise that I had to do this:
[doc updateChangeCount:UIDocumentChangeDone];
[doc closeWithCompletionHandler:nil];
Obviously if a file is open for writing, then it would not be wise to allow it to be deallocated!
Doh! Hopefully this saves someone some time in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With