2

次のように定義されたコアデータモデルがあります。

ユーザーには多くのイベントがあります。各イベントには多くの写真を含めることができます。関係には「カスケード」削除ルールがあります。

エンティティが消えたときにローカルファイルを削除する方法を理解しようとしています。コアデータエンティティがなくなる前に呼び出す、ある種のdeallocまたは「finalize」メソッドはありますか?

各画像エンティティには、アプリのドキュメントディレクトリに保存されているローカルファイルへの参照があります。

テーブルビューのcommitEditingStyleを使用してユーザーを削除する場合、関係をステップ実行して画像を削除し、ファイルを手動で削除できます。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    {
        // Delete the managed object for the given index path
        NSManagedObjectContext *context = [[[RKObjectManager sharedManager] objectStore] managedObjectContext];

        AppUser* managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];

        //clean up local files before deleting the object
        [self deleteLocalContentForAppUser:managedObject];        

//now delete the object
        [context deleteObject:managedObject];

        // Save the context to remember deletion
        NSError* error = nil;
        if (![context save:&error]) {
            /*
             Replace this implementation with code to handle the error appropriately.

             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }

    }

}

ただし、エンティティが他のメソッドによって削除され、フェッチされた結果コントローラーに通知が届くと、ネストされた関係にはオブジェクトが含まれません。ユーザーのイベントセットには、ローカルコンテンツをステップスルーして削除するエンティティはありません。これは、リレーションシップに設定された「カスケード」削除ルールの結果ですか?

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
//    UITableView *tableView = self.tableView;
//     
    AppUser* managedObject = anObject;
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone];
//             [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            //             [self configureCell:[tableView cellForRowAtIndexPath:newIndexPath] atIndexPath:newIndexPath];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];


            //these methods cannot find any content to delete
            [managedObject deleteLocalImages];
            [managedObject deleteLocalContent];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[self.tableView cellForRowAtIndexPath:newIndexPath] atIndexPath:newIndexPath];

            [managedObject updateLocalImages];
            break;

        case NSFetchedResultsChangeMove:
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}
4

1 に答える 1

4

サブクラスにprepareForDeletionメソッドを実装できます。NSManagedObjectオブジェクトが削除される前に自動的に呼び出されるため、そこから参照ファイルを削除することもできます。

于 2012-05-02T18:05:21.013 に答える