4

我一直在努力弄清楚如何在我的核心数据和我的 SQL 数据库之间建立连接。该数据库未预先填充,但在启动时将在许多不同的用户中使用。

斯坦福大学提供的 Core Data & iCloud 课程我已经学习了 3 次,但无济于事。我还彻底研究了 iOS 中的 SQLite/SQL 关系数据库,但无济于事。我唯一的其他选择就是来到这里,发布我能发布的所有代码,并祈祷有人会为我解答。所以这里...

AppDelegate.m

- (void)saveContext
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {

            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

#pragma mark - Core Data stack

- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return _managedObjectContext;
}

- (NSManagedObjectModel *)managedObjectModel
{
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [self applicationDocumentsDirectory];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"shindy.sqlite"];

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _persistentStoreCoordinator;
}

#pragma mark - Application's Documents directory

- (NSURL *)applicationDocumentsDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"shindy.sqlite"];

    return [NSURL fileURLWithPath:path];
}

主视图控制器.m

- (void)setupFetchedResultsController
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Shindy"];
    request.sortDescriptors = [NSArray arrayWithObjects:
                               [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES],
                               [NSSortDescriptor sortDescriptorWithKey:@"dateAndTime" ascending:YES],
                               [NSSortDescriptor sortDescriptorWithKey:@"photo" ascending:YES],
                               [NSSortDescriptor sortDescriptorWithKey:@"details" ascending:YES],
                               [NSSortDescriptor sortDescriptorWithKey:@"timePosted" ascending:YES],
                               [NSSortDescriptor sortDescriptorWithKey:@"location" ascending:YES],
                                                                                                    nil];

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

}

- (void)fetchShindyDataIntoDocument:(UIManagedDocument *)document
{
    dispatch_queue_t fetchQ = dispatch_queue_create("Shindy Fetcher", nil);
    dispatch_async(fetchQ, ^{
        NSArray *shindys = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
        NSLog(@"shindys = %@", shindys);
        [document.managedObjectContext performBlock:^{
            for (NSDictionary *shindyInfo in shindys) {
                [Shindy shindyWithShindyDBInfo:shindyInfo inManagedObjectContext:document.managedObjectContext];
                NSLog(@"fire");
            }
        }];
    });
}

- (void)useDocument
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"shindy.sqlite"];

    if (![[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]) {
        [self.shindyDatabase saveToURL:self.shindyDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            [self setupFetchedResultsController];
            [self fetchShindyDataIntoDocument:self.shindyDatabase];
        }];
    } else if (self.shindyDatabase.documentState == UIDocumentStateClosed) {
            [self.shindyDatabase openWithCompletionHandler:^(BOOL success) {
        [self setupFetchedResultsController];
        }];
    } else if (self.shindyDatabase.documentState == UIDocumentStateNormal) {
        [self setupFetchedResultsController];
    }
}

- (void)setShindyDatabase:(UIManagedDocument *)shindyDatabase
{
    [self useDocument];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self fetchShindyDataIntoDocument:self.shindyDatabase];

    if (!self.shindyDatabase) {
        [self setShindyDatabase:self.shindyDatabase];
    }
}

Shindy+ShindyDB.m

+ (Shindy *)shindyWithShindyDBInfo:(NSDictionary *)shindyInfo
            inManagedObjectContext:(NSManagedObjectContext *)context
{
    Shindy *shindy = nil;

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Shindy"];

    NSSortDescriptor *dateAndTimeSort = [NSSortDescriptor sortDescriptorWithKey:@"dateAndTime" ascending:YES];
    NSSortDescriptor *detailsSort = [NSSortDescriptor sortDescriptorWithKey:@"details" ascending:YES];
    NSSortDescriptor *locationSort = [NSSortDescriptor sortDescriptorWithKey:@"location" ascending:YES];
    NSSortDescriptor *nameSort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    NSSortDescriptor *photoSort = [NSSortDescriptor sortDescriptorWithKey:@"photo" ascending:YES];
    NSSortDescriptor *timePostedSort = [NSSortDescriptor sortDescriptorWithKey:@"timePosted" ascending:YES];
    // title
    request.sortDescriptors = [NSArray arrayWithObjects:dateAndTimeSort, detailsSort, locationSort, nameSort, photoSort, timePostedSort, nil];

    NSError *error = nil;
    NSArray *matches = [context executeFetchRequest:request error:&error];

    if (!matches || ([matches count] > 1)) {
        // handle error
    } else if ([matches count] == 0) {
        shindy = [NSEntityDescription insertNewObjectForEntityForName:@"Shindy" inManagedObjectContext:context];

        shindy.dateAndTime = [shindyInfo objectForKey:@"dateAndTime"];
        shindy.details = [shindyInfo objectForKey:@"details"];
        shindy.location = [shindyInfo objectForKey:@"location"];
        shindy.name = [shindyInfo objectForKey:@"name"];
        shindy.photo = [shindyInfo objectForKey:@"photo"];
        shindy.timePosted = [shindyInfo objectForKey:@"timePosted"];
        // title
        // Guestlist? The rest?
        // Use below for reference
        shindy.whoseShindy = [User userWithName:[shindyInfo objectForKey:@"whoseShindy"] inManagedObjectContext:context];
    } else {
        shindy = [matches lastObject];
    }

    shindy = [NSEntityDescription insertNewObjectForEntityForName:@"Shindy" inManagedObjectContext:context];
    shindy.dateAndTime = [shindyInfo objectForKey:@"dateAndTime"];
    shindy.details = [shindyInfo objectForKey:@"details"];
    shindy.location = [shindyInfo objectForKey:@"location"];
    shindy.name = [shindyInfo objectForKey:@"name"];
    shindy.photo = [shindyInfo objectForKey:@"photo"];
    shindy.timePosted = [shindyInfo objectForKey:@"timePosted"];

    return shindy;
}

添加ShindyViewController.m

- (void)saveShindyToDatabase
{
    NSArray *shindys = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
    [self.shindyDatabase.managedObjectContext performBlock:^{
        for (NSDictionary *shindyInfo in shindys) {
            [Shindy shindyWithShindyDBInfo:shindyInfo inManagedObjectContext:self.shindyDatabase.managedObjectContext];

            NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
            // url = [url URLByAppendingPathComponent:@"Default Shindy Database"];
            self.shindyDatabase = [[UIManagedDocument alloc] initWithFileURL:url];

            [self.shindyDatabase setValue:self.detailView.text forKey:@"details"];

            if (FBSession.activeSession.isOpen) {
                [[FBRequest requestForMe] startWithCompletionHandler:
                    ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                        if (!error) {
                            self.name = user.name;
                            self.photo.profileID = user.id;
                            self.username = user.username;
                        }
                    }];
            }
            [self.shindyDatabase setValue:self.name forKey:@"name"];
            [self.shindyDatabase setValue:self.photo forKey:@"photo"];
            [self.shindyDatabase setValue:self.username forKey:@"username"];
            // [Guest guestWithName:self.name username:self.username photo:self.photo inManagedObjectContext:self.shindyDatabase.managedObjectContext];
            [self.shindyDatabase setValue:self.locationManager.location forKey:@"location"];
            [self.shindyDatabase setValue:self.dateAndTimePicker.date forKey:@"dateAndTime"];
        }
    }];
}

我知道我要的东西太多了,但我已经用尽了我可以使用的每一个资源。如果有人能指出我正确的方向,我将永远感激不尽!

4

1 回答 1

1

如果您计划使用 CoreData 映射现有数据库,您将不会成功。事实上,您无法使用 CoreData 映射任何数据库。

Core Data 实际上是一个图形对象管理,具有不同的存储选项,而 sqllite 只是其中一种可能性。在启动时,如果您选择 sqllite 作为选项,您的应用程序将创建一个具有特定表结构的数据库。如果您尝试连接到不是由 CoreData 框架创建的数据库,您将收到错误消息。

您可以做的是摆脱 CoreData,并构建您的 NSObject 以充当实体。但是,您将不得不实现所有逻辑来保存、更新、版本控制、并发 ecc ......这是一个漫长(且有缺陷)的方式,尤其是在多用户环境中。

否则,如果我误解了你的问题,请告诉我。也许,您可以发布错误堆栈跟踪。

于 2012-12-19T14:42:12.020 回答