If an iOS user is actively adding data to a view, what is the best way to retrieve this from the server?

StackOverflow https://stackoverflow.com/questions/23588112

Вопрос

I've created a quick application that lists Hotels and Rooms. I've created a backend with Rails and postgresql, and I've got it up on heroku. I'm using JSON to pass information back and forth betwene my iOS front end.

Here's what I'm wondering. Let's say you have a TableViewController with a UIBarButtonItem for adding new items. The new item is added, and the view should update.

How do you do this? Right now my app retrieves from the server again which seems stupid.

I might already have 50 items in the list. If I add a new item, the server is hit again to now retrieve 51 items.

What's the best way to go about this?

Это было полезно?

Решение

Do two things simultaneously.

  1. Send the POST request to make the new item.
  2. Add the item to the UITableView

Such as:

self.listings addObject:myObject];
[self.tableView reloadData];

The best thing to do is to assume that the data was sent successfully, and show the listing in the UITableView as soon as possible. This will provide for a fast and fluid experience. 95% of the time, the data will be sent to your server fine. On the off chance that an error occurred, you can simply let the user know something went wrong and give them option to try again (save it locally, and if they go back to the add screen after an error, prepopulate the data).

The other, lazier and worse experience option, is after they hit "add", you show a loading indicator and wait for the server response before adding it to the UITableView and refreshing the tableview.

But again - the data you send to the server should be the same you are displaying locally*, so just create the object and add it to the array.

Note: * If the server does a lot of work with this data and what you send is not the same as to what you display, your server should respond with a 201 created and the created object. Serialize the response and add that to the UITableView.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top