UIDocument永不调用dealloc [英] UIDocument never calling dealloc
问题描述
我遇到一个问题,我似乎无法取消分配UIDocument(用于iCloud)
I have an issue where I can't seem to dealloc UIDocument (used in iCloud)
运行NSMetaDataQuery查找文档后,如下所示.
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];
我处理我的查询
- (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];
}];
}
}];
}
}
NSLog(@"iCloud document opened %@", doc);
为每个UIDocument显示一个不同的内存地址.
The NSLog(@"iCloud document opened %@", doc);
shows a different memory address for each UIDocument.
我的UIDocument子类中有一个NSLog,但从未调用过它.我看不到它没有被释放的保留位置.每当我想要同步我的云数据时,都会运行此查询,这种情况相当定期发生.数据正确同步.
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.)
我认为我的UIDocument泄漏了,在这种假设下我是正确的,这是我第一次与iCloud进行角力,所以我在一些事情上仍然一无所知.
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;
我没有使用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!
Do!希望这可以节省某人将来的时间.
Doh! Hopefully this saves someone some time in the future.
这篇关于UIDocument永不调用dealloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!