Basic data for SQL does not work

I'm trying to get the hell out of how to connect my master data and my SQL database. The database is not pre-populated, but it will be used among many different users when it is launched.

I went through the Core Data and iCloud classes provided by Stanford three times to no avail. I also looked at SQLite / SQL relational databases in iOS to no avail. The only other option I have is to come here, post all the code that I can, and pray that someone has an answer to me. So here it is ...

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

HomeViewController.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; } 

AddShindyViewController.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"]; } }]; } 

I know what I'm asking, it's a hell of a lot, but I have exhausted every single resource that I have. If anyone can even point me in the right direction, I will be forever grateful!

+4
source share
1 answer

If you plan to map an existing database to CoreData, you will not work. Unable to specify any database using CoreData.

Core Data infact is graph object management, with a different storage option, and sqllite is just one of the features. At startup, if you select sqllite as an option, your application will create a database with a specific table structure. If you try to connect to a database that was not created by the CoreData framework, you will receive an error message.

What you can do is get rid of CoreData and build your NSObject to act as entities. But then you have to implement all the logic for saving, updating, versioning, concurrency ecc ... and this is a long (and buggy) way, especially in a multi-user environment.

Otherwise, tell me if I did not understand your question. Perhaps you can post an error stack trace.

+1
source

All Articles