Embed master data in an existing iPhone project

I have some problems implementing Core Data for my existing iPhone project. First I want to give you a more detailed overview:

  • Some of my classes are nested: the Game class has an NSArray with objects of the Player class, the player class has an NSArray with objects of the Item class in turn.

  • What I want to do is save an instance of my upstream Game class (for example, when I exit my application).

I tried some tutorials about Core Data, but there are some more questions:

  • Should I create an object for each of my classes or just for the game?
  • If I need to do this for each of them: I think I will have to create ALL the relationships between my classes, but: How to create relationships, for example. between “Games” and “Player” (please remind: I have many players in ONE NSArray).
  • How about changing my existing project? What I have already done is copying the missing methods into my AppDelegate. But what should I do with my classes, especially with the Getter / Setter methods? Just change "@synthesize" to "@dynamic" in the implementation?

I hope for some light in my darkness;)

Thank you so much now

Mac1988

+4
source share
2 answers

I recommend setting up your database model in xcode, and then when you do this, select the objects and select File> New File from the menu. Then select the Managed Feature Class from the Cocoa touch class. After "Next", select where to save the files, and in the next step Xcode will ask you which objects should be generated for the files.

After you have done this, you can implement the functions that you need, for example, you delegate. My recommendation is to leave your existing things as they are and use the main data classes as their own. Just pull the data you want from existing classes / arrays and put them into the database as needed. When extracting, on the contrary ... get them from the database and add them to your functions / classes.

An example from one of my projects:

.H file

@class quicklistSet; @interface rankedAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> { [...] NSMutableArray *_searchHistory; NSMutableArray *_quickList; } [...] @property (nonatomic, retain) NSMutableArray *_searchHistory; @property (nonatomic, retain) NSMutableArray *_quickList; /* Quicklist functions */ - (void)addToQuicklist:(quicklistSet *)theQuicklistSet; - (BOOL)checkIfQuicklistExists:(quicklistSet*)theQuicklistSet; - (NSMutableArray *)getQuicklists; - (void)deleteQuicklist:(NSNumber*)theAppId; @end 

.M file

 #import "quicklistSet.h" #import "quicklist.h" @implementation rankedAppDelegate @synthesize window; @synthesize tabBarController; @synthesize _searchHistory, _quickList; [...] /* Quicklist functions */ - (void)addToQuicklist:(quicklistSet *)theQuicklistSet { BOOL exists = [self checkIfQuicklistExists:theQuicklistSet]; if(!exists) { quicklist *theQuicklist = (quicklist *)[NSEntityDescription insertNewObjectForEntityForName:@"quicklist" inManagedObjectContext:self.managedObjectContext]; [theQuicklist setAppName:[theQuicklistSet _appName]]; [theQuicklist setAppId:[theQuicklistSet _appId]]; [theQuicklist setAppImage:[theQuicklistSet _appImage]]; [theQuicklist setCountryId:[theQuicklistSet _countryId]]; [theQuicklist setCategoryId:[theQuicklistSet _categoryId]]; [theQuicklist setLastCheck:[theQuicklistSet _lastCheck]]; [theQuicklist setLastRank:[theQuicklistSet _lastRank]]; [_quickList addObject:theQuicklist]; [self saveAction]; } else { NSLog(@"Existing quicklistSet: %@", [theQuicklistSet _appName]); } } - (BOOL)checkIfQuicklistExists:(quicklistSet*)theQuicklistSet { // Get the categories NSMutableArray *quicklistArray = [self getQuicklists]; BOOL exists = NO; for(quicklist *dbQuicklist in quicklistArray) { if([[dbQuicklist appId] isEqualToNumber:[theQuicklistSet _appId]]) { exists = YES; continue; } } return exists; } - (NSMutableArray *)getQuicklists { if(_quickList == NULL) { NSLog(@"Array is null"); NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"quicklist" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; NSError *error; NSArray *items = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] retain]; NSMutableArray *returnArray = [[[NSMutableArray alloc] initWithArray:items] retain]; _quickList = returnArray; [fetchRequest release]; } else { NSLog(@"Not null. Count: %d", [_quickList count]); } return _quickList; } - (void)deleteQuicklist:(NSNumber*)theAppId { NSLog(@"Delete row"); // Create a new managed object context for the new book -- set its persistent store coordinator to the same as that from the fetched results controller context. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"quicklist" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"appId=%@",theAppId]; [fetchRequest setPredicate:predicate]; NSError *error; NSArray *items = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; [fetchRequest release]; if([items count] > 0) { NSManagedObject *eventToDelete = [items objectAtIndex:0]; [self.managedObjectContext deleteObject:eventToDelete]; [self saveAction]; } } /* END Quciklist functions */ [...] @end 

EDIT: QuicklistSet was my existing class, quicklist is my coredata class.

+2
source
  • Yes, you want to create an entity for all the classes you mentioned.

  • You have already received the answer to this question in your question: make a one-to-many relationship. For example, for a game’s game relationship, click the To Many Relationships check box in the data model editor.

  • You want your data classes (Game, Player, Item) to inherit from NSManagedObject. You probably want to remove all instance variables that match the attributes added to Core Data. For many relationships (players, elements), you will definitely want to get rid of the NSArray member variable that you used. Instead, make it as if you are talking and creating @ dynamic accessors for the properties of players and objects. Note that you want to use NSSet instead of NSArray for players and elements.

For example, the title for your game class might look like this:

 @interface Game : NSManagedObject { } @property(nonatomic, retain) NSSet *players @property(nonatomic, retain) NSString *someOtherProperty; @property(nonatomic, retain) NSNumber *yetAnotherProperty; @end 

And then your implementation file might look like this:

 #import "Game.h" @implementation Game @dynamic players, someOtherProperty, yetAnotherProperty; - (void)awakeFromInsert { // do initialization here } // other methods go here @end 

In addition, be careful when changing the properties of players and objects. There are a lot of good details in the Using Managed Objects section of the Master Data Programming Guide, but for the most part, to add a Player instance to the game you will

 [game addPlayerObject:newPlayer]; 

To create a new player, you would do something like:

 NSManagedObject *newPlayer = [NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:context]; 
+1
source

All Articles