3

UIDocument (iCloud で使用) の割り当てを解除できないように見える問題があります。

NSMetaDataQuery を実行して、次のようにドキュメントを検索した後..

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];

}

次に、新しいドキュメントをロードまたは作成します。

- (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 ごとに異なるメモリ アドレスを示します。

UIDocument サブクラスに NSLog がありますが、呼び出されません。リリースしていないことがどこに保持されているのかわかりません。このクエリは、クラウド データを同期するたびに実行されます。これはかなり定期的に行われます。データは正しく同期されます。

デバッグに何も表示されず、アプリが単にダッシュボードに閉じられるという奇妙なクラッシュが発生しています (以前の経験から、アプリがメモリを消費しすぎて終了することがよくあります)。

私のUIDocumentが漏れていると思います.この仮定で正しいでしょうか.iCloudと格闘したのはこれが初めてなので、いくつかのことでまだ暗闇の中にいます.

私のサブクラスには次のプロパティがあります。

@property (copy, nonatomic) NSData *infoData;
@property (copy, nonatomic) NSMutableArray *firstArray;
@property (copy, nonatomic) NSMutableArray *secondArray;
@property (copy, nonatomic) NSMutableArray *thirdArray;

私はARCを使用していません。

4

1 に答える 1

6

私はこれをしなければならないことに気づきませんでした:

        [doc updateChangeCount:UIDocumentChangeDone];
        [doc closeWithCompletionHandler:nil];

明らかに、ファイルが書き込み用に開かれている場合、そのファイルの割り当てを解除できるようにするのは賢明ではありません!

どっ!うまくいけば、これは将来誰かの時間を節約します。

于 2012-07-12T16:04:05.393 に答える