I created an object that contains properties that in the end will manage data that lives in core data.
I would like to create methods within this object to set data that would be saved into core data as well as method that gets all data from core data and sets properties in the object so I can have everything contained in one object.
The object I am using to set the object’s properties is not setting it properly. It will set them within part of the method but in another part of the method and outside of the method the properties are null.
It is a scoping problem that I clearly do not have my head around so I am turning to the experts. Please advise where I have gone astray. Thanks again for your assistance in advance!
Ed
Object Definitions:
.m file
@implementation GenObject
@synthesize name = _name;
@synthesize address = _address;
- (void)getGenData
{
MyAppDelegate *genDataAppDelegate = [[UIApplication sharedApplication] delegate]; // Create a variable to the app delegate
NSManagedObjectContext *genDataContext = [genDataAppDelegate managedObjectContext]; // Create a context
NSEntityDescription *getDataEntityDesc = [NSEntityDescription entityForName:@"GeneralData" inManagedObjectContext:genDataContext]; // Create a description for the core data
NSFetchRequest *genDataRequest = [[NSFetchRequest alloc] init]; // Create a Fetch request for the core data
[genDataRequest setEntity:getDataEntityDesc]; // Set the fetch requests entity description
NSManagedObject *genDataMatches = nil; // Create a managed object
NSError *genDataCoreError; // Create an error object
NSArray *genDataCoreObjectsArray = [genDataContext executeFetchRequest:genDataRequest error:&genDataCoreError]; // Fetch the core data and load the objects array
int genDataCoreObjectsCount = [genDataCoreObjectsArray count]; // Get the count of the objects fetched
if ([genDataCoreObjectsArray count] == 0) // Check to see if there were any objects fetched
{
//NSLog(@"No matches");
}
else // If there were objects fetched then loop over them
{
for(int y = 0; y < genDataCoreObjectsCount; y++) // Loop over the objects fetched
{
genDataMatches = [genDataCoreObjectsArray objectAtIndex:y]; // Get the current object
self.name = [[genDataMatches valueForKey:@"name"] retain];
self.address = [[genDataMatches valueForKey:@"address"] retain];
NSLog(@"BASE URL %@",self.baseurl); // WORKS HERE!!!
}
}
NSLog(@"BASE URL %@",self.baseurl); // DOES NOT WORK HERE!!!
}
@end
.h file
@interface GenObject : NSObject
{
NSString *_name;
NSString *_address;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *address;
- (void)setGenIntitalData;
@end
And if I try to call it from within a different class like say a ViewController as such…
NOTE: Within the viewController genDataObj is a property of the viewController
- (void)viewDidLoad
{
self.genDataObj = [GenData new];
[self.genDataObj setGenIntitalData]; // Setup the gendata core data
[self.genDataObj getGenData]; // Set up the gen data from the core data
NSLog(@"NAME: %@", self.genDataObj.name); // NAME IS nil HERE!!!!
[super viewDidLoad];
}
You should confirm that your NSManagedObject is actually returning data. Try this:
In
setGenIntitalDataare you saving your changes to the persistent store? I assume that insetGenIntitalDatayou are creating initial settings; but if theNSManagedObjectisn’t saved, then your subsequent fetch will yield invalid data.