How exactly does fetchAllIfNeeded differ from fetchAll in the JS SDK?

I never understood the necessary part of the description.

.fetchAll() Selects the specified Parse.Object list.

.fetchAllIfNeeded() Selects the specified Parse.Object list, if necessary.

In what situation can I use this and what exactly determines the need? I feel that this is something super elementary, but I could not find a satisfactory and clear definition.

In an example in the API, I noticed that fetchAllIfNeeded() has:

 // Objects were fetched and updated. 

If successful, when fetchAll has only:

 // All the objects were fetched. 

So fetchAllIfNeeded() also saves stuff? Very confusing here.

UPDATES

TEST 1

Turning to some @danh hints left in the comments, I tried the following things.

 var todos = []; var x = new Todo({content:'Test A'}); // Parse.Object todos.push(x); x.save(); // So now we have a todo saved to parse and x has an id. Async assumed. x.set({content:'Test B'}); Parse.Object.fetchAllIfNeeded(todos); 

So, in this case, my client x is different from the server. But x.hasChanged() is incorrect, since we used the set function, and the change event is fired. fetchAllIfNeeded does not return results. Thus, this does not mean that he is trying to compare it directly with what is being synchronized and retrieved on the server.

I noticed that in the request payload, running fetchAllIfNeeded sends the next interesting thing.

 {where: {objectId: {$in: []}}, _method: "GET",…} 

So it seems like something on the clientide determines if the isNeeded object

Test 2

So, now, based on the comments, I tried to manipulate the changed state of the object, setting it to silent.

 x.set({content:'Test C'}, {silent:true}); x.hasChanged(); // true Parse.Object.fetchAllIfNeeded(todos); 

Still nothing interesting. Obviously, the server state ("Test A") is different from the client ("Test C"). and I still get [] , and the request payload:

 {where: {objectId: {$in: []}}, _method: "GET",…} 

UPDATE 2

I found this out by looking at the source of Parse. See the answer.

+5
source share
1 answer

After many manipulations, looking at the source, I realized this. Basically, fetchAllIfNeeded will retrieve the models in an array that has no data, which means the absence of attribute properties and values.

Thus, if used, you can specify the parent object with an array of Parse nested objects. When you select a parent object, nested child objects in the array are not included (unless you have a restriction on the include request). Instead, pointers are sent back to clients and to your client, these pointers are translated into "empty" models without data, basically just empty Parse.Objects objects with identifiers.

In particular, Parse.Object has an internal boolean property called _hasData , which seems to switch to any true time material, such as set, or fetch, or something that gives these model attributes.

So, let's say you need to get these child objects. You can just do something like

 var childObjects = parent.get('children'); // Array Parse.Object.fetchAllIfNeeded(childObjects); 

And he will look for those children who are currently presented only as empty objects with an identifier.

It’s useful, unlike fetchAll , in that you can go through the children array and lazily load one at a time as needed, and then in the future you need to “get the rest”. fetchAllIfNeeded essentially just filters the “rest” and sends a whereIn request that restricts the selection of child objects that have no data.

In the Parse documentation, they have the answer in the callback response to fetchAllIfNeeded as:

 // Objects were fetched and UPDATED. 

I think they mean that customer objects have been updated. fetchAllIfNeeded definitely sends GET calls, so I doubt any updates on the server. So this is not some kind of synchronization function. This really confused me, as I immediately thought about updating the servers when they really mean:

 // Client objects were fetched and updated. 
+7
source

All Articles