Note that there are some explanatory texts on larger screens.

plurals
  1. POFetched managedObject return null
    primarykey
    data
    text
    <p>I created an IBAction that I link to saveButton, when I call all core data methods inside the IBAction it works fine in following code:</p> <pre><code>- (IBAction)saveButtonTap:(id)sender{ NSManagedObjectContext *context = [self managedObjectContext]; NSManagedObject *object = [NSEntityDescription insertNewObjectFor:@"Person" inManagedContext:context]; [self setValue:firstNameTextField.text forKey:@"firstName"]; [self setValue:lastNameTextField.text forKey:@"lastName"]; NSError *error = nil; if (![context save:&amp;error]) NSLog(@"Can't save transaction - %@ || %@ ", error, [error localizedDescription]); } </code></pre> <p>And when I separate the core data saving methods as the below, the saved firstName and lastName, showing null when fetched. It didn't return error, but I have no idea after googling all day. Could someone point anything wrong with my code?</p> <p><strong>Person.h + Person.m</strong></p> <pre><code>@interface Person : NSManagedObject @property(strong) NSString *firstName; @property(strong) NSString *lastName; - (void)save; @end @implementation Person @synthesize firstName; @synthesize lastName; - (void)save{ [self setValue:self.firstName forKey:@"firstName"]; [self setValue:self.lastName forKey:@"lastName"]; } @end </code></pre> <p><strong>mainViewController.h + mainViewController.m</strong></p> <pre><code>@interface mainViewController : UIViewController @property(strong) ManagedObjectContext *context; @property(strong) Person *person; - (IBAction)saveButtonTap:(id)sender; @end @implementation mainViewController @synthesize context; @synthesize person; - (void)viewDidLoad{ // ... some view did Load rituals if(context == nil){ context = [self managedObjectContext]; // Assume this method calls for managed object context from shared application. } if(person == nil){ person = [NSEntityDescription insertNewObjectFor:@"Person" inManagedContext:context]; } } - (IBAction)saveButtonTap:(id)sender{ person.firstName = firstNameTextField.text; person.lastName = lastNameTextField.text; [self.person save]; NSError *error = nil; if (![context save:&amp;error]) NSLog(@"Can't save transaction - %@ || %@ ", error, [error localizedDescription]); } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload