iOS Core Data to-many relationship insert/fetch

Paulius Dragunas

In my application I have trip, which has many stops. This has been specified in the .xcdatamodeld file. I can go ahead an manipulate trips in any manner that I want and they all work okay. However, I am running into issues with adding many stops to each trip. It does not seem to persist. Here is the following code. this code is inside of a a detailViewController of a trip, so a specific trip has already been clicked on.

Trip * trip = [self retrieveObjectWithID:_passedObjectId];


    StopOff *newStop = [NSEntityDescription insertNewObjectForEntityForName:@"StopOff" inManagedObjectContext:managedObjectContext];

    [newStop setValue:stopName forKey:@"stopName"];
    [newStop setValue:stopCity forKey:@"stopCity"];
    [newStop setValue:stopDate forKey:@"stopDate"];
    [newStop setValue:stopAddress forKey:@"stopAddress"];
    [newStop setValue:stopState forKey:@"stopState"];
    [newStop setValue:stopTime forKey:@"stopTime"];

    newStop.trip = trip;
    [trip addStopObject:newStop];

    NSLog(@" stop count %i", [trip.stop count]);

Here is what my console spits out the first time i hit a button to run this: CoreData: annotation: to-many relationship fault "stop" for objectID 0x8bb2810 <x-coredata://4CE70783-4729-46E0-B18B-8E325D1020CC/Trip/p20> fulfilled from database. Got 0 rows 2013-11-24 21:19:43.417 Tracker[30633:70b] stop count 1

If I keep hitting the button, stop count increases. If I relaunch the app the stop count goes back down and starts over again, so it seems that it is not persisting.

My question is, how exactly do I insert many stops that correspond to a trip, and once they are inserted and persist, how do I go ahead and get every corresponding stop to that trip.

Here is the code for which retrieves each trip just fine. and managedObjectContext is handled in the parent view controller using NSFetchedResultsController. Please let me know if you need anymore information

- (Trip*)retrieveObjectWithID:(NSManagedObjectID*)theID
{
    NSError *error = nil;
    Trip *theObject = (Trip*)[self.managedObjectContext existingObjectWithID:theID error:&error];
    if (error)
        NSLog (@"Error retrieving object with ID %@: %@", theID, error);
    return theObject;
}
Dan Shelly

You are not saving your context after applying the changes (new StopOff objects).

After adding a StopOff, call [managedObjectContext save:&error] and you will persist your new object to the store.

CoreData does not save automatically, and without a save: being called, you will loose any temporal changes made to the context.

also, there is no need for [trip addStopObject:newStop]; as CoreData is taking care of that as part of the inverse relationship.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Many to many relationship to self in Core Data (iOS)

From Dev

To-Many Relationship with iOS Core Data

From Dev

iOS core data one to many relationship NSSET

From Dev

IOS/Core-Data: add many to many relationship

From Dev

iOS: core data update object with one- with to-many relationship

From Dev

Core data and one to many relationship

From Dev

iOS Core data Many-to-Many relationship difference between addRelationshipObject and addRelationship?

From Dev

How do I set up a many-to-many relationship in iOS? (Core Data)

From Dev

Core Data add object to-many relationship

From Dev

Core-Data NSFetchedResultsController to-many relationship

From Dev

Core Data - sectionNameKeyPath with a One to Many Relationship

From Dev

Core data one to many relationship in swift

From Dev

Core-Data NSFetchedResultsController to-many relationship

From Dev

Core Data - Order of objects in a to-many relationship

From Dev

Core Data to-many relationship filterContentForSearchText with NSPredicate

From Dev

Core Data one-to-many inverse relationship

From Dev

Correct Delete Rule for Core Data Many-to-Many Relationship?

From Dev

Core Data Join Table Many to Many Relationship - Object Creation

From Dev

Core Data Many-to-Many relationship update causes fault

From Dev

Allowing duplicate records in Core Data Many-to-many relationship

From Dev

Best practice for preloading core data with many to many relationship

From Dev

How to make a many to many relationship in core data in the .xcdatamodel

From Dev

Core Data: Fetching related objects in many-to-many relationship

From Dev

iOS: core data, number of relationship with Auto ID

From Dev

saving, deleting and fetching data from a one to many relationship core data

From Dev

How to insert and fetch to-many relationship entities in core data

From Dev

Getting objects from to-many relationship in Swift Core Data

From Dev

Core Data: sorting by date attribute in a to-many relationship

From Dev

Core data save in Background with one-to-many relationship

Related Related

  1. 1

    Many to many relationship to self in Core Data (iOS)

  2. 2

    To-Many Relationship with iOS Core Data

  3. 3

    iOS core data one to many relationship NSSET

  4. 4

    IOS/Core-Data: add many to many relationship

  5. 5

    iOS: core data update object with one- with to-many relationship

  6. 6

    Core data and one to many relationship

  7. 7

    iOS Core data Many-to-Many relationship difference between addRelationshipObject and addRelationship?

  8. 8

    How do I set up a many-to-many relationship in iOS? (Core Data)

  9. 9

    Core Data add object to-many relationship

  10. 10

    Core-Data NSFetchedResultsController to-many relationship

  11. 11

    Core Data - sectionNameKeyPath with a One to Many Relationship

  12. 12

    Core data one to many relationship in swift

  13. 13

    Core-Data NSFetchedResultsController to-many relationship

  14. 14

    Core Data - Order of objects in a to-many relationship

  15. 15

    Core Data to-many relationship filterContentForSearchText with NSPredicate

  16. 16

    Core Data one-to-many inverse relationship

  17. 17

    Correct Delete Rule for Core Data Many-to-Many Relationship?

  18. 18

    Core Data Join Table Many to Many Relationship - Object Creation

  19. 19

    Core Data Many-to-Many relationship update causes fault

  20. 20

    Allowing duplicate records in Core Data Many-to-many relationship

  21. 21

    Best practice for preloading core data with many to many relationship

  22. 22

    How to make a many to many relationship in core data in the .xcdatamodel

  23. 23

    Core Data: Fetching related objects in many-to-many relationship

  24. 24

    iOS: core data, number of relationship with Auto ID

  25. 25

    saving, deleting and fetching data from a one to many relationship core data

  26. 26

    How to insert and fetch to-many relationship entities in core data

  27. 27

    Getting objects from to-many relationship in Swift Core Data

  28. 28

    Core Data: sorting by date attribute in a to-many relationship

  29. 29

    Core data save in Background with one-to-many relationship

HotTag

Archive