Parse SDK – persistence of array relationships (Android)

I went through a great deal of pain trying to create an elegant model for one of my Android apps. The recommended solution for 1 to many relationships (one list with many items) is to use columns of Array type.

But then the Android Parse SDK documentation (for version 1.7.1) does not specifically call out arrays in the Relational Data section. Yet the Updating Object section does mention them.

Reading carefully about arrays I then noticed this warning:

Note that it is not currently possible to atomically add and remove items from an array in the same save. You will have to call save in between every different kind of array operation.

Great, double-save is just what I needed 😦 At this point I felt that the Parse Android documentation is suboptimal for my needs – not that they’d be any special. Still, this app was meant to be just a slightly more advanced prototype. So I marched on.

Given the small number of referenced items I wanted to rely on arrays to automatically fetch them. To nicely bridge between a fixed sized array and a dynamically resizing list I wanted to just populate the array from a list prior to saving it.

This is what worked for me in one step, without needing to save in-between:

  public void setItems(List todoItems) {
    remove(ITEMS_KEY); // Remove any previously referenced items

Deletion of removed items is handled separately in the model and UI so the lack of cascade delete was not a problem for me.

Of course, consider this an undocumented feature that could break at any time. As of today this works with Android Parse SDK 1.7.1.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s