1
votes

I have been trying to develop a Core Data piece of code and have been running into problems. In one version of my code everything works but after i have been trying to polish the code, i have the old version, and suddenly it does not work. I have not changed this class, which is why i am puzzled.

In the original version i get the output that can be seen in the bottom of this post, this is only one line, which is the database. In this version i get nothing.

I have a feeling it can be some problems with the connection between the app and Core Data. I did do a rename of the application via "Project -> Rename" so maybe that is the reason for this problem? I am afraid i do not know where to look.

EDIT: Just saw that the xcdatamodel have the old name!

EDIT2: Did rename a copy of the old version and got the same problem so i would guess the reason for this problem is the rename of the application. Now i am wondering how to align the Core Data database with the new name?

// Define qContext
NSManagedObjectContext *qContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"questions" inManagedObjectContext:qContext];
[fetchRequest setEntity:entity];

NSArray *fetchedObjects = [qContext executeFetchRequest:fetchRequest error:&error];

for (NSManagedObject *info in fetchedObjects) {
...

2011-01-24 20:58:38.969 FamQuiz_v2[27431:207] fetchedObjects: ( " (entity: questions; id: 0x7088740 ; data: )", ...

=====================================

=============== UPDATE ==============

I did test if there is any data in the other parameters with the following code:

//=========PREPARE CORE DATA DB===========//
if (managedObjectContext == nil) { managedObjectContext = [(FamQuiz_v2AppDelegate *)
                                                           [[UIApplication sharedApplication] delegate] managedObjectContext]; }
// Define qContext
NSManagedObjectContext *qContext = [self managedObjectContext];

NSLog(@" ");
NSLog(@"qContext: %@", qContext);
NSLog(@" ");

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"questions" inManagedObjectContext:qContext];
NSLog(@" ");
NSLog(@"entity: %@", entity);
NSLog(@" ");

[fetchRequest setEntity:entity];

NSLog(@" ");
NSLog(@"fetchRequest: %@", fetchRequest);
NSLog(@" ");




//>>>>ERROR COMING HERE


NSArray *fetchedObjects = [qContext executeFetchRequest:fetchRequest error:&error];
NSLog(@"fetchedObjects: %@", fetchedObjects);
NSLog(@"====STOP====");

The result is below and indicate that i do have access to the database, or? But still no "fetchedObjects":

2011-01-24 22:27:54.094 FamQuiz_R0_2[29523:207] qContext:
2011-01-24 22:27:54.095 FamQuiz_R0_2[29523:207]
2011-01-24 22:27:54.095 FamQuiz_R0_2[29523:207]
2011-01-24 22:27:54.120 FamQuiz_R0_2[29523:207] entity: () name questions, managedObjectClassName NSManagedObject, renamingIdentifier questions, isAbstract 0, superentity name (null), properties {
idQ = "(), name idQ, isOptional 0, isTransient 0, entity questions, renamingIdentifier idQ, validation predicates (\n), warnings (\n), versionHashModifier (null), attributeType 200 , attributeValueClassName NSNumber, defaultValue (null)";
qDiff = "(), name qDiff, isOptional 0, isTransient 0, entity questions, renamingIdentifier qDiff, validation predicates (\n), warnings (\n), versionHashModifier (null), attributeType 200 , attributeValueClassName NSNumber, defaultValue (null)";
qNrAccess = "(), name qNrAccess, isOptional 0, isTransient 0, entity questions, renamingIdentifier qNrAccess, validation predicates (\n), warnings (\n), versionHashModifier (null), attributeType 200 , attributeValueClassName NSNumber, defaultValue (null)";
qRegDate = "(), name qRegDate, isOptional 0, isTransient 0, entity questions, renamingIdentifier qRegDate, validation predicates (\n), warnings (\n), versionHashModifier (null), attributeType 900 , attributeValueClassName NSDate, defaultValue (null)";
qRightAnswer = "(), name qRightAnswer, isOptional 0, isTransient 0, entity questions, renamingIdentifier qRightAnswer, validation predicates (\n), warnings (\n), versionHashModifier (null), attributeType 700 , attributeValueClassName NSString, defaultValue (null)";
qWrongAnswer1 = "(), name qWrongAnswer1, isOptional 0, isTransient 0, entity questions, renamingIdentifier qWrongAnswer1, validation predicates (\n), warnings (\n), versionHashModifier (null), attributeType 700 , attributeValueClassName NSString, defaultValue (null)";
qWrongAnswer2 = "(), name qWrongAnswer2, isOptional 0, isTransient 0, entity questions, renamingIdentifier qWrongAnswer2, validation predicates (\n), warnings (\n), versionHashModifier (null), attributeType 700 , attributeValueClassName NSString, defaultValue (null)";
question = "(), name question, isOptional 0, isTransient 0, entity questions, renamingIdentifier question, validation predicates (\n), warnings (\n), versionHashModifier (null), attributeType 700 , attributeValueClassName NSString, defaultValue (null)";
}, subentities {
}, userInfo {
}, versionHashModifier (null)
2011-01-24 22:27:54.121 FamQuiz_R0_2[29523:207]
2011-01-24 22:27:54.123 FamQuiz_R0_2[29523:207]
2011-01-24 22:27:54.123 FamQuiz_R0_2[29523:207] fetchRequest: (entity: questions; predicate: ((null)); sortDescriptors: ((null)); type: NSManagedObjectResultType; )
2011-01-24 22:27:54.124 FamQuiz_R0_2[29523:207]
2011-01-24 22:27:54.125 FamQuiz_R0_2[29523:207] fetchedObjects: (
)
2011-01-24 22:27:54.126 FamQuiz_R0_2[29523:207] ====STOP====

===========UPDATE2==========

Here the code is:

- (NSManagedObjectModel *)managedObjectModel {

if (managedObjectModel_ != nil) {
    return managedObjectModel_;
}
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"FamQuiz_v2" ofType:@"momd"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
return managedObjectModel_;

}

=============== UPDATE-3 ==============

I am really puzzled about this problem but did test to rename the app back to the original name and then i worked. For me this means that there is a connection somewhere that i cannot find, and i have been looking.

I REALLY would like to understand how to rename an app with Core Data without loosing the connection to the database.

I would very much appreciate if anyone could spread some light on this as i would need to rename it.

=============== UPDATE-4 ==============

Just to give an update on this problem, I never succeeded to get this thing working so i decided to use the 'opportunity' to rewrite the whole application. May sound like a harsh way of solving this problem but as being very new to programming it also allowed me to clean up the code.

I am still very interested how to solve the problem.

3
According to your log the managedObjectContext is printing nothing. Take a look at the FamQuiz_v2AppDelegate for this method: - (NSManagedObjectModel *)managedObjectModel or where ever you create your NSManagedObjectModel. Might be worth posting that with your answer?v01d

3 Answers

1
votes

In a terminal navigate into the project directory and actually cd into the .xcodeproj file itself. Edit the project.pbxproj file with vi and change all the occurrences of the former name to the updated one. It seems that Xcode doesn't know to update all of the Core Data references, but this worked for me.

0
votes

You don't say which Mac OS you are targeting (Mac OS X or iOS), but this sounds like an iOS issue. In the case of iOS, a Core Data store is static between runtimes; if a Core Data already exists from a previous run time, it will automatically use that despite any changes in your code. Try cleaning all targets before building again.

0
votes

I had this same problem and finally fixed it by creating a new Data Model.

  1. Delete: MyApplication.xcdatamodeld
  2. Create a new one: File > New > File... > Core Data > Data Model
  3. Add my entities back

After adding back my entities, everything worked again.