How to handle CRUD operation with Diffable Data Source & NSFetchedResultsController - ios

I have a simple product store screen of 4 rows with each row containing 4-5 products.
Currently I have the following setup
Controller calls the backend server and gets the data
Save the models to Core Data
This triggers an update of NSFetchedResultsController
Apply the new snapshot using Diffable Data Source
This setup works great when new products are added to any row or are updated in some way.
But I am unable to find a way on how to handle the scenario when a product is removed in the API response ? because whenever I receive a response I save it in Core Data base and then rely on the FRC trigger to apply the snapshot.
So If an existing product is removed in the response the product still continues to show in the store as it also needs to be removed in the DB as well. So before saving into DB I always have to delete all the existing data and then save the new data for changes to take effect.
Can anybody suggest a change in my setup to handle this scenario or any particular flow to handle it ?

Related

Dynamics365 Operations: Created/Updated timestamps with Data Entities

I am new to Dynamics FnO, and recently followed the articles to access data through oData, and was successful.
What I see missing in the data objects that I normally receive in integrations out of the Microsoft World is the created/updated timestamps.
I am trying to put a synchronous data flow from FnO to my NodeJs application, so that my app keeps polling data from FnO whenever there is a change. This can be achieved easily if there were timestamps with the data that flows in.
Is there a way to setup those timestamps somewhere?
You have to make sure that the underlying table that you are querying has the fields added on it, and also that the data entity you are accessing through odata has the fields setup up on it as well.
Make sure this is setup on the table:
And then you have to drag and drop the field(s) from the datasource field list to the exposed field list in the data entity:
After this, you will have these fields

Ember - Initializing table data from ember-data then updating via websocket

I have a table of data that I want to update dynamically.
When the user goes to the page, I want to initialize the table with data from my rails backend. Easy with the model hook and ember data.
I then want to keep this information refreshed using the connected websocket stream.
How should I manage this. Should I be updating the model with the websocket updates (without committing the data to the backend)? The table data is an object array in the component, should I just initialize this from the model setupController function then keep the array updated directly?
Is there an easy way to map the websocket data JSON into the model or table array?
Yes you should be able to do this with Ember Data. Caveat: I have not tried this.
Somewhere you are opening your websocket stream and adding an "onmessage" handler. In that handler, you will receive the payload from the server, where you can use store.pushPayload() to update the record in the store. If the record (identified by the id field) is already in the store, it will be replaced. Otherwise it will be added as a new record. If you are displaying the record in the current template, you should just see the values change when the new record is pushed.
This Ember guide page describes this scenario, where you are streaming data from the server and you want to see instant updates to the user interface.
Additional reading: the API for pushPayload

Swift - Refresh UITableView but keep current position in table

I'm trying to create a planning app with Swift 2, which pulls the data from a remote server via an API. The data of the API is ordered by time. The app shows the data in the exact same order as the API returns.
Now, however, I have created a UITableView which can be pulled to refresh the data. I also have added the possibility to rearrange the data to the likings of the user. When the user refreshes the table however, the data will all be restored to the order of the returned API data.
My question is: how can I still refresh the data, but let the data keep the current position in the table?
You control the order in which data is displayed by how you implement cellForRowAtIndexPath. The API doesn't control your UI.
If you choose to display the data in the order returned by the API, then yes, user will lose their custom arrangement when the table is reloaded.
If you want the user to control the order data is displayed, you will need to persist some information about the order so that you can keep that order when the table is reloaded.
In the simplest case, you could provide some default sort options that make sense for the kind of data you have. You might have a type or category value that makes sense to sort on. You would then only need to persist the sort options the user has chosen and sort your data when reloading the table.
If you want the user to have complete control over the order, then you need to persist that order and reapply it when the table is reloaded. This is considerably more challenging. Some options, from easiest to hardest:
1. You could persist the order locally using just an identifier that uniquely identifies each item.
2. You could persist all the data and the order locally.
3. You could persist the order on the server.

How to avoid an unsaved entity while fetching data in Core Data?

This is my first project where I am using Core Data with sqlite as the backing store.
Here are the quick details of the scenario:
There is a feedback form that gets filled in one screen.
There is a screen where I can see saved forms.
The form data can be synced with back-end server.
I am using MKNetworkingKit for interacting with REST API. (Looks like I should have looked at RestKit, but I dont have time to go back)
When I save the form I save the data in the main managedObjectContext of the application to the persistentStore.
In the form screen I have a sync button that syncs the application data with backend.
Also while saving the data locally, I check for connectivity and push the rest of the unsynced data to the server.
In the screen where I have saved forms, there also I have a sync button to sync data.
My Problem is that in the screen while I am filling a form and the form info in entity is not complete, and its in an inconsistent state.
I use the same method of my dataManager singleton to do the syncing.
In other screens where I sync data, my managed object context is in consistent state and I can sync the data, but while filling the form I want to avoid the entity I am working on and have not saved it.
What should I be doing now to get things done quickly?
Also what should be the ideal way of designing such application using core data?
Don't create the actual entity until the form data is complete and validated. If you need an intermediate place to store it while editing is happening, invent an object with the same data fields but which isn't a managed object. (Java people used to do this regularly with the Data Transfer Object pattern.)

CoreData Entity Updates at App Login

My app talk to webServer. At login, I pull down JSON and make up CoreData with 4 Entities (about 1000 rows each). The data changes on Server, So with every login, I have to update my existing CoreData.
What is the best approach to find out if records exist and insert new ones if need be?
To be smart on update (not blindly update every time), you need some intelligence on the server side.
One idea I would do.
Server has master table that records the timestamp of modified date of the 4 entities. It also has API to expose the master table. Every time change occurs to one of the 4 entities, master table's corresponding entry has to be updated as well.
You create the same copy of master table in application side as well.
On application launch, you query API in 1. and compare with the value in 2. to see if the timestamp has updated on the server side.
If YES, then download and replace the corresponding entity.
Another one which allows finer control.
Add timestamp column to the 4 entities on the server side. Every time entry is added/updated, the timestamp is updated.
Prepare an API for each entity that filters only newer items than the specified timestamp
On application launch, you query API in 2. and update.
The hole of the second approach is that it cannot handle deletion on the server side. Maybe you can combine something like the first approach to support this.

Resources