在我的应用程序中,我有旅行,其中有很多站点。这已在 .xcdatamodeld 文件中指定。我可以以任何我想要的方式进行操纵旅行,而且它们都可以正常工作。但是,我遇到了为每次旅行添加许多站点的问题。它似乎没有持续存在。这是以下代码。此代码位于行程的 detailViewController 中,因此已单击特定行程。
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]);
这是我第一次按下按钮运行它时我的控制台吐出的内容: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
如果我继续按下按钮,停止计数会增加。如果我重新启动应用程序,停止计数会回落并重新开始,所以它似乎没有持续存在。
我的问题是,我究竟如何插入与旅行相对应的许多停靠点,一旦它们被插入并持续存在,我如何继续并获得该旅行的每个相应停靠点。
这是可以很好地检索每次旅行的代码。并managedObjectContext
在父视图控制器中使用NSFetchedResultsController
. 如果您需要更多信息,请告诉我
- (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;
}