IOS CoreData: how to set up a basic data “connection”? Stanford UIDocument vs Apple pattern?

I worked on the Stanford course and set up my first applications and basic data, as in a lecture. In approximately the same way (now I will transfer the code to the application delegate):

- (void)setupFetchedResultsController
{
    NSError *error = nil;
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MainCategory"];
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"position" ascending:YES]];
    [self.budgetDatabase.managedObjectContext executeFetchRequest:request error:&error];

    self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:request
                                                                       managedObjectContext:self.budgetDatabase.managedObjectContext
                                                                         sectionNameKeyPath:nil
                                                                                  cacheName:nil];
}

-(void)useDocument
{
    if(![[NSFileManager defaultManager]fileExistsAtPath:[self.budgetDatabase.fileURL path]]){
        [self.budgetDatabase saveToURL:self.budgetDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            [self setupFetchedResultsController];
        }];
    } else if (self.budgetDatabase.documentState == UIDocumentStateClosed){
        [self.budgetDatabase openWithCompletionHandler:^(BOOL success){
            [self setupFetchedResultsController];
        }];
    } else if (self.budgetDatabase.documentState == UIDocumentStateNormal){
        [self setupFetchedResultsController];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    //Initialize database
    [super viewWillAppear:animated];
    if(!self.budgetDatabase){
        NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentationDirectory inDomains:NSUserDomainMask] lastObject];
        [url URLByAppendingPathComponent:@"BudgetDatabase"];
        self.budgetDatabase = [[UIManagedDocument alloc]initWithFileURL:url];
    }
}

The code in the Apple template (if you install "CoreData" when creating the Xcode project) looks very different and more complicated. What is the difference with this UIManagedDocument? Are there any better or worse?

+4
source share
3 answers

UIManagedDocument , , . , Core Data, "" . , .

UIManagedDocument. Apple, , Core Data. , , ( ), .

UIManagedDocument , , . .

Update

UIManagedDocument Core Data , .

, ( ), . , .

, , , . , ( , ..), . UX.

, , , .

? . , 6 , Core Data, , UIManagedDocument.

? , ?

, .

- . .

+2

. UIManagedDocument Core Data Stack.

UIManagedDocument . , iCloud. Core Data.

. UIManagedDocument . . UIDocument.

Core Data Stack . . UIManagedDocument .

Core Data . , , Apple, Core Data Stack @jrturton. , 6 , .

.

, NSFetchedResultsController, . , UIViewController, UITableViewController , (.. UIViewController).

, .

+1

UIDocument, , , . , coredata , // .

Apple, . appDelegate. - Multi-Context CoreData.

0

All Articles