Howdy,
I have an issue with populating coredata from an XML load. From my limited knowledge it appears to be a memory issue, I am looking squarely at the managed object constructer in the for loop in the pseudo-code below!
We can assume 'myManagedStore' is a valid coredata object with an NSSet of "managedObjects" and that 'myNSSetOfItems' is a NSMutableDictionary of key/value pairs generated from my XML parse.
Note: I start debugging with the application and data deleted from the simulator.
for (NSMutableDictionary *item in myNSSetOfItems){
MyManagedObject *managedObject = [MyManagedObject mangedObjectInContext];
managedObject.key = item.value;
...
...
NSLog(@"log managedObject to show me its key/value pairs here");
[myManagedStore addManagedObject:managedObject];
}
Logging the managedObject at the store injection point shows it to be a 'MyManagedObject' with valid key/value pairs from the for-loop initialiser.
Next, I quit the app and start it again. I run a debug routine to check the contents of the store
for (MyManagedObject *managedObject in storedSet){
// the following finds the managedObject OK
NSLog(@"Found managedObject as %@",managedObject);
// but accessing any properties reveals the error
NSLog(@"A property of managedObject is %@",[managedObject myProperty]);
}
The first iteration is fine, it finds managedObject and logs correctly. The second iteration is hectic. Both random and inherently wrong - in one instance I saw it log
Found managedObject as managed/com.apple.SystemConfiguration/kCFPreferencesCurrentUser
This immediately suggests to me that I have a memory issue - it looks like my managedObject in the first code block isn't being closed or finalised correctly.. The problem is, I don't know why!
The code for [MyManagedObject mangedObjectInContext] is a simple convenience routine in the managed object, I have had no previous issues with this approach
+ (id)managedObjectInContext:(NSManagedObjectContext*)context {
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ManagedObject" inManagedObjectContext:context];
ManagedObject *managedObject = [[ManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:context];
return [managedObject autorelease];
}
Sorry if the question is dumb, I am new to ObjC and iOs development.