I am seeing some interesting behavior when attempting to access and modify the same PFObject
from within two different view controllers.
I have one view controller that has a strong property for the PFObject
. When I present the second view controller, it has a weak property of the same type, so I set that property equal to the first controller's property. When I change the data of the PFObject
in the second view controller, it is updated in the first view controller because essentially it is the same object, I presume. This is working really well.
Now if I try to set the PFObject
equal to nil
in the second controller, when I go back to the first controller the PFObject
is still defined - it's not nil
. Why is that? I need to ensure the second view controller is editing the exact same object the first controller owns.
Would it be better to store a property to the first controller itself in the second controller and instead reference that public property instead of trying to use two properties for the same object, or what would be recommended in this case?
//First controller
@property (nonatomic, strong) PFObject *myObject;
//prepare for segue
//ensure myObject is not nil first, otherwise alloc init, then
secondController.myObject = self.myObject;
//Second controller
@property (nonatomic, weak) PFObject *myObject;
//somewhere in the code
self.myObject[SomeKey] = SomeValue; //works great, updates myObject in both controllers
self.myObject = nil; //doesn't affect the first controller's myObject
In the second controller, you're setting its reference of the PFObject
to nil
. This does not affect the object itself or the first view controller as it still has a reference to the object.
It would be better to define a data model that is accessible by any view controller, but not exclusively owned by any. That data model could then send notifications and if a view controller were interested, it could listen and respond accordingly.
As a simple example, think about how Parse implemented the current user. You just reference [PFUser currentUser]
from any part of your code. You could do something similar with [MyAppData currentWidget]
, or obviously whatever data needs to be accessible.
And to further the example, the setter for currentWidget
could post an NSNotification
such as WidgetHasChangedNotification
. View Controllers could then add themselves as observers to that notification.