Access Parse Object ID right after instantiation - ios

I'm creating two PFObjects at the same time that should reference each other's object IDs when they're saved. In the example below, the second object is supposed to save the first object's object ID in an array.
let objectForFirstClass = PFObject(className:"ClassOne")
let objectForSecondClass = PFObject(className: "ClassTwo")
objectForSecondClass.setObject([objectForFirstClass.objectId!], forKey: "classOneObjectArray")
The last line is causing the error because objectForFirstClass.objectId is nil. I'd assume this is because the object hasn't been saved yet. How can I fix this?

You want to save after creating the first object, and in the completion handler, create the second one with a reference to the first one.
You can use saveAllInBackground:block: for this.

Correct, the object id is assigned by the server when saved. I'd be tempted to write some cloud code to do what you want so you can send some details and the cloud code will create and connect the objects, then return both of them to you. You can of course do the same thing locally in your app, there's just more network comms.
You should also consider using pointers or relationships. These are better for querying, though the same save requirements apply before you can set the connections.

Related

Can I specify a objectId when creating new PFObjects?

Swift / iOS
Can anyone tell me if it is possible to specify the PFObject objectId value when creating new objects?
I've obviously attempted but the save fails. (which might just be the answer)
The reason I am asking is I wondered if anyone had found a "trick" that would allow me to specify.
I am using PFObject.saveInBackground { method to persist the new object.
No you can not. Parse sets the objectId on the server during the save operation.
The reason your operation is failing is because Parse is looking for an object on the server with the id that you are specifying and is then trying to update that object but it cannot find the object.

Fix uneccessary copy of NSManagedObject

I'm sorry the title may mislead you, since I'm not so good at English. Let me describe my problem as below (You may skip to the TL;DR version at the bottom of this question).
In Coredata, I design a Product entity. In app, I download products from a server. It return JSON string, I defragment it then save to CoreData.
After sometimes has passed, I search a product from that server again, having some interaction with server. Now, I call the online product XProduct. This product may not exist in CoreData, and I also don't want to save it to CoreData since it may not belong to this system (it come from other warehouse, not my current warehouse).
Assume this XProduct has the same properties as Product, but not belong to CoreData, the developer from before has designed another Object, the XProduct, and copy everything (the code) from Product. Wow. The another difference between these two is, XProduct has some method to interact with server, like: - (void)updateStock:(NSInteger)qty;
Now, I want to upgrade the Product properties, I'll have to update the XProduct also. And I have to use these two separately, like:
id product = anArrayContainsProducts[indexPath.row];
if ([product isKindOfClass:[XProduct class]] {
// Some stuff with the xproduct
}
else {
// Probably the same display to the cell.
}
TL;DR
Basically, I want to create a scenario like this:
Get data from server.
Check existed in CoreData.
2 == true => add to array (also may update some data from server).
2 == false => create object (contains same structure as NSManagedObject from JSON dictionary => add to array.
The object created in step 4 will never exist in CoreData.
Questions
How can I create an NSManagedObject without having it add to NSMangedObjectContext and make sure the app would run fine?
If 1 is not encouragement, please suggest me a better approach to this. I really don't like to duplicate so many codes like that.
Update
I was thinking about inheritance (XProduct : Product) but it still make XProduct the subclass of NSManagedObject, so I don't think that is a good approach.
There are a couple of possibilities that might work.
One is just to create the managed objects but not insert them into a context. When you create a managed object, the context argument is allowed to be nil. For example, calling insertNewObjectForEntityForName(_:inManagedObjectContext:) with no context. That gives you an instance of the managed object that's not going to be saved. They have the same lifetime as any other object.
Another is to use a second Core Data stack for these objects, with an in-memory persistent store. If you use NSInMemoryStoreType when adding the persistent store (instead of NSSQLiteStoreType), you get a complete, working Core Data stack. Except that when you save changes, they only get saved in memory. It's not really persistent, since it disappears when the app exits, but aside from that it's exactly the same as any other Core Data stack.
I'd probably use the second approach, especially if these objects have any relationships, but either should work.

Core Data Different Object ID For First Time

I am using Entity's Object ID in order to uniquely identify local notifications and modify them. I observed that first time when I save my entity, it has following object ID:
<x-coredata:///Task/tE1C5A230-A419-42D5-AF78-3327A09D13BD2>
If I don't exit my application, and try to modify notification, object ID doesn't change and I can modify my notification.
Now, if I restart my app and try to access that entity again, it has different object ID:
<x-coredata://D6703834-ECB4-487B-84F8-330A215E16B7/Task/p13>
So I can't modify notification, as object ID for entity is different. Interesting thing is whenever I access that entity, Object ID remains same as the last one.
So my Question here is why Core data shows different object ID for the first time entity is created? When I try to access entity after opening app again for many times, the object ID (different than the first one) remains constant. I am curious to know why is it happening so?
Please note:
I know there are many posts on SO pointing out that using Object ID is not a reliable approach. Still I want to know reason that why two IDs are being shown.
the first OID is a temporary OID - a temporary id denotes objects that have not been saved yet. the 2nd id is a permanent one and is assigned to a MO AFTER it has been saved:
so...
var objectID = object.objectID
if objectID.temporaryID {
object.managedObjectContext.save() //try do catch left out
}
objectID = object.objectID
assert(objectID.temporaryID == false)

Save a Parse Object Causes Another Object Saved Too

I am quite new with Parse, I am confused on how parse works.
Here I have a PFUser, PFGroupObject, PFUserGroupObject. Where PFUserGroupObject is a "Table" that relate user an group.
PFUserGroupObject and PFGroupObject is my custom PFObject
What confuses me is that, once I save the PFUserGroupObject, the PFGroupObject is also saved to Parse.
I have 2 questions:
Is this behaviour normal? I know that PFUserGroupObject has a variable that stores PFGroupObject. But, I am wondering if this is the expected behaviour of Parse.
//MARK - Create a New Group
var group: PFGroupObject = PFGroupObject();
group.name = "Friends Group";
//MARK - Create a Many to Many Join Table to Relate User and Group
var userGroup: PFUserGroupObject = PFUserGroupObject();
userGroup.user = PFUser.currentUser();
userGroup.group = group;
//THIS LINE CAUSES MY GROUP OBJECT TO BE SAVED TO PARSE AS WELL
PFObject.save(userGroup);
Let's say I called
PFObject.saveAll([group, userGroup]);
Would the group object saved twice into Parse?
I am trying to understand how Parse works.
Thank you!!!!!
When you save a "Parent" object that has one or more "child" objects, it will save the children as well. I have never worked with PFGroupObjects, but I have a pointer to a custom object attached to each of my users. Rather than having to save my custom object, then save my user (or vice versa), saving just my user will also save my custom object.
There are some nuances with this, such as trying to add a pointer to an unsaved object which you have just created to another object, then saving the parent. While you generated an object id when you created the child object, you haven't actually saved it, so it's a dangling pointer.
Hope this helps!
edit - If you want to see exactly what is getting saved when, add a before save trigger to your cloud code for the object you want to see whether or not is being saved multiple times, with a console print line inside. Do some testing, and check your logs to see how many times the beforeSave trigger is getting called, and that's how many times you are attempting to save an object of that type.

Do I have to fetch the object first in order to update a meta-data field in parse.com

In Parse.com, the help document for updating an object seems to require a query first to retrieve the object before one can update the meta data.
Given that I know the objectId of the object I am updating, do I still have to make this additional call to retrieve everything else about the object before I can send an update call (via saveInBackground)?
Yes, you do. In order to update an object from parse, you must have a local copy.
In order to make a call to update an object - which can take several forms of save or saveInBackground or saveEventually, etc. - you must first make a query to have a copy of the object.
If you know the objectId, is sounds like you have already run a query on an associated object - why not get the full object then with an includeKey: call? https://www.parse.com/docs/ios_guide#queries-relational/iOS

Resources