Hobbyist
Hobbyist

Reputation: 16202

Limiting and Sorting with Parse?

I'm trying to learn how to use Parse and while it's very simple, it's also... not? Perhaps I'm just missing something, but it seems like Parse requires a lot of client-side code, and even sending multiple requests for a single request. For example, in my application I have a small photo gallery that each user has. The images are stored on Parse and obtained from parse when needed.

I want to make sure that a user can not store any more than 15 images in their gallery at a time, I also want these images to be ordered by an index.

Currently it seems like the only viable option is to perform the following steps on the client:

This is a total of 3 or? 6 requests just to upload a file, depending on if a "response" is considered a request by parse too. This also does not provide any way to order the pictures in the gallery. Would I have to create a custom field called "index" and set that to the number of photos received in the first query + 1?

Upvotes: 0

Views: 32

Answers (1)

danh
danh

Reputation: 62676

It's worse than you think: to create the picture you must create a file, save it, then save a reference to the file in an object and save that, too.

But it's also better than you think: this sort of network usage is expected in a connected app, and some of it can be mitigated with additional logic on the server ("cloud code" in parse parlance).

First, in your app, consider a simple data model where _User has an array of images (represented, say, by an "UserImage" custom class). If you keep this relationship as an array of pointers on user, than a user's images can be fetched eagerly, when the app starts, so you'll know the image count as a fact along with the user. The UserImage object will have a file reference in it, so you can optionally fetch the image data and just hold the lighter metadata with the current user.

Ordering is a more ephemeral idea. One doesn't order objects as they are saved, but rather as they are retrieved. Queries can be ordered according to any attribute, and even more to the point, since you're retrieving all 15 images, you should consider ordering them for presentation a function of the UI, not the data.

Finally, parse limits your app not by transaction count, but by transaction rate, with a free limit low enough to serve plenty of users.

Upvotes: 1

Related Questions