This question may seem long, but I'm sure it's relativity simple for Core-Data experts. Showing the configuration made this Question long. Thanks!
In CoreData I have a User
entity and an NSManagedObject
subclass (User.h/.m)
created from that entity. Not sure if it's relevant, but I'm using remote Database with Stackmob.
Here is what my fetch request looks like in Review.m:
Fetch Request:
-(NSArray *)fetchRequest
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
//Please Note the line below:
[fetchRequest setReturnsObjectsAsFaults:NO];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username == %@", self.usernameField.text];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
return fetchedObjects;
}
if (fetchedObjects.count>=1)
{
NSLog(@"Number of Objects Returned %i", fetchedObjects.count);
NSManagedObject *fetchedObject = [fetchedObjects objectAtIndex:0];
}
Here is the log:
2013-08-18 10:26:25.082 Time[995:c07] Number of Objects Returned 1
2013-08-18 10:26:25.082 Time[995:c07] User Object: <User: 0xe07b260> (entity: User; id: 0xa65ab70 <x-coredata://392983AD-D649-4D68-A93F-D86109BC009C-995-000004B584F1BB06/User/piphone5> ; data: <fault>)
Several weeks ago I had created the User Object successfully with the following:
User *newUser = [[User alloc] initIntoManagedObjectContext:self.managedObjectContext];
[newUser setValue:self.usernameField.text forKey:[newUser primaryKeyField]];
[newUser setValue:self.emailAddressField.text forKey:@"email"];
[newUser setPassword:self.passwordField.text];
[newUser setValue:self.gender forKey:@"gender"];
[self.managedObjectContext saveOnSuccess:^{
[self.client loginWithUsername:self.usernameField.text password:self.passwordField.text onSuccess:^(NSDictionary *results) {
NSLog(@"Login Success %@",results);
} onFailure:^(NSError *error) {
NSLog(@"Login Fail: %@",error);
}];
This is what I would like to do in Review.m:
@interface Review()
@property (strong, nonatomic) User *user;
@end
@sythesize = user;
....
if (fetchedObjects.count>=1)
{
NSLog(@"Number of Objects Returned %i", fetchedObjects.count);
NSManagedObject *fetchedObject = [fetchedObjects objectAtIndex:0];
NSLog(@"fetchObject Object %@", fetchedObject);
//Addition
user = (User*)(fetchedObject);
NSLog(@"User %@", user);
}
Here is the log:
2013-08-18 11:07:13.313 Time[1177:c07] Number of Objects Returned 1
2013-08-18 11:07:13.314 Time[1177:c07] fetchObject Object <User: 0xa532cc0> (entity: User; id: 0xb426de0 <x-coredata://3336122B-7117-4D92-B0A1-DDBAF80DDBF7-1177-000006EEE046E326/User/piphone5> ; data: <fault>)
2013-08-18 11:07:13.314 Time[1177:c07] User <User: 0xa532cc0> (entity: User; id: 0xb426de0 <x-coredata://3336122B-7117-4D92-B0A1-DDBAF80DDBF7-1177-000006EEE046E326/User/piphone5> ; data: <fault>)
Why is this not working? User still shows up as Fault? I have the following line in fetch Request: [fetchRequest setReturnsObjectsAsFaults:NO];
The Following is the reason I need to do this:
Notification *notificationObject = [NSEntityDescription insertNewObjectForEntityForName:@"Notification" inManagedObjectContext:self.managedObjectContext];
[notificationObject setValue:[NSNumber numberWithInt:1] forKey:@"appType"];
[notificationObject setValue:[notificationObject assignObjectId] forKey:[notificationObject primaryKeyField]];
NSError *error = nil;
if (![self.managedObjectContext saveAndWait:&error])
{
NSLog(@"There was an error");
}
else
{
NSLog(@"Notification Object Created");
}
//I need to Add the fetchedObject as UsersObject (which is a relationship)
[notificationObject addUsersObject:user];
[self.managedObjectContext saveOnSuccess:^{ ....
Here is my CoreData Setup:
Why is this not working? User still shows up as Fault? I have the following line in fetch Request: [fetchRequest setReturnsObjectsAsFaults:NO];
This is a StackMob restriction. In https://developer.stackmob.com/ios-sdk/core-data-guide
it is explicitly stated that returnObjectsAsFaults
/ setReturnObjectsAsFaults:
is not (yet) supported.