Japa
Japa

Reputation: 632

Pass core data entity variable between views

I´m having trouble understanding how to use a core data entity variable between views, and for better understanding of what my issue is, my code is below:

View A:

At some point i´m doing this when a save button is pressed:

- (void)guardarOrcamento
{
newBudget=[NSEntityDescription insertNewObjectForEntityForName:@"Budget"  inManagedObjectContext:context];

newBudget.relationshipBetweenEntityBudgetAndClient = myEntityClientVariable;

UIAlertView *saved = [[UIAlertView alloc]initWithTitle:@"Budget Saved" message:@""     delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];

[saved show];

NSError *error;
[context save:&error])
}

View B:

My problem is in this view, i need to connect another relationship and for that, my "newBudget" variable most not be empty!:

- (void) setSelectedRowInTableview:(int)line

{ 
rowEntity=[NSEntityDescription insertNewObjectForEntityForName:@"rowEntity"  inManagedObjectContext:context];
rowEntity.relationshipBetweenEntityRowEntityAndBudget = newBudget;

....

This RowEntity can only exist if Budget entity already exists...and at this point it does!...in the other view i have inserted a new object and saved it...and i understand why the variable "newBudget"(in view B) is empty, but how can i persist it?

Thanks for you time

Upvotes: 0

Views: 1102

Answers (1)

nzs
nzs

Reputation: 3252

Basically you have to pass either the full budget entity or the ID of the relevant budget entity from view "A" to view "B". Not knowing your app's view hierarchy and logic I assume you select a budget then add entities related to that budget.

Independently from the variable passing solution you have the choice whether you store the selected/inserted budget object in an NSManagedObject variable or store the ID of the budget object in NSManagedObjectID and then retrieve the object using -(NSManagedObject *)existingObjectWithID:(NSManagedObjectID *)objectID error:(NSError **)error.

1) using global variable

Setup in your AppDelegate a NSManagedObject/NSManagedObjectID, and make it accesable:

NSManagedObject *selectedBudgetReference; // OR
NSManagedObjectID *selectedBudgetReferenceID;
...
@property (strong, nonatomic) NSManagedObject *selectedBudgetReference;
@property (strong, nonatomic) NSManagedObjectID *selectedBudgetReferenceID;

Then store into the inserted/selected reference at view A:

AppDelegate *app = (AppDelegate*) [[UIApplication sharedApplication] delegate];
app.selectedBudgetReference = newBudget;
app.selectedBudgetReferenceID = [newBudget objectID];

Finally access it in view B:

AppDelegate *app = (AppDelegate*) [[UIApplication sharedApplication] delegate];
NSManagedObject *localBudgetToRelate = app.selectedBudgetReference;
NSManagedObject *localBudgetToRelate2 = [context existingObjectWithID:app.selectedBudgetReferenceID];

2) passing variable when user switches from view A to B

Similarly as above but you setup the object variable in form B (formBViewController) and when on form A and creating form B to switch to that view you basically access the form B's newly created view controller and pass the budget info to formBViewController's object variable.

Upvotes: 1

Related Questions