views:

27

answers:

1

Hello

i just first tried out core data right now.

I created an App which loads Locations from a Server and creates by using a JSON-Parser dynamically Model-Instances of Class "Location". Works fine and no problems.

Now i need to store some locations as favorizedLocations localy on the client. Therefore i want to use CoreData. I had set up a LocationsDetailView with a Button "Add To favorites". And the next things which are supposed to happen are the "Unkown Things" now.

- (void)addFavorite{
LogForGUI(@"TODO Add Favorit with name %@ to Core Data", **objTheLocation**.strAdr);
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

// If appropriate, configure the new managed object.
[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];...

I have created a Location.xcdatamodel the Location.h Modelclass. I had to remove the Arrays and Dictionaries within the xcdatamodel because they required a Type and Array was not available (obviously).

So what i want: I want to bring my already created Model Instance in the CoreData World without setting all properties.

And second question: how can i handle arrays and dictionaries with core data.

+2  A: 

First, you don't handle arrays and dictionaries in Core Data. Core Data is your object graph.

As for your first question, what do you mean by your model instance? If you created a xcdatamodel, it will get compiled into a mom file and placed in your Resources folder. Using the template Core Data code you can then load that mom file and create the Core Data stack.

If you are not referring to the xcdatamodel then please clarify your question.

Update

I strongly suggest that you re-read the Core Data documentation because your implementation is not going to work.

Marcus S. Zarra
I created a Model Class Location with all Attributes. When parsing my JSON response i create instances of Location, add them to an array, and show them on a map. The marker on the map link to a detailview which is populated from this model instance (entity). That was all done before i added CoreData. I just want to save this Location as Favorite by using CoreData.
grobald
@grobald -- You can't save any random object in Core Data, you can only save NSManagedObjects and NSManagedObject subclasses. You need to create an entity for Location in your data model and (optionally) a NSManagedObject subclass for Location. Then you create Location objects by inserting them in the managed object context. Then you populate each object with the data. Then you save the context.
TechZen