How to sync CoreData and a REST web service asynchronously and the same time properly propagate any REST errors into the UI

前端 未结 4 780
别跟我提以往
别跟我提以往 2020-12-07 07:03

Hey, I\'m working on the model layer for our app here.

Some of the requirements are like this:

  1. It should work on iPhone OS 3.0+.
  2. The source of
4条回答
  •  离开以前
    2020-12-07 07:22

    You need a callback function that's going to run on the other thread (the one where actual server interaction happens) and then put the result code/error info a semi-global data which will be periodically checked by UI thread. Make sure that the wirting of the number that serves as the flag is atomic or you are going to have a race condition - say if your error response is 32 bytes you need an int (whihc should have atomic acces) and then you keep that int in the off/false/not-ready state till your larger data block has been written and only then write "true" to flip the switch so to speak.

    For the correlated saving on the client side you have to either just keep that data and not save it till you get OK from the server of make sure that you have a kinnf of rollback option - say a way to delete is server failed.

    Beware that it's never going to be 100% safe unless you do full 2-phase commit procedure (client save or delete can fail after the signal from the server server) but that's going to cost you 2 trips to the server at the very least (might cost you 4 if your sole rollback option is delete).

    Ideally, you'd do the whole blocking version of the operation on a separate thread but you'd need 4.0 for that.

提交回复
热议问题