Multithreading with master data and API requests

Introduction

I have read many lessons and articles on Core Data concurrency, but I have a problem that is not often covered or addressed in the real world, and I hope someone can help her. I checked related questions in SO and no one answered this specific question that I can find.

Background

We have an existing application that retrieves data from the API (in the background thread) and then saves the records returned to the main data. We also need to display these entries in the application at that time.

So, the process that we are currently running is as follows:

  • Make a network request for data (background)
  • Perform data analysis and map objects to NSManagedObjects and save (background)
  • In the completion handler (main thread), we retrieve the records from the main data with the same order and limit that we requested from the API.

Most concurrency master data guides follow this save pattern in one thread and then retrieved in another, but most of them give examples such as:

NSArray *listOfPeople = ...;
[NSManagedObjectHelper saveDataInBackgroundWithContext:^(NSManagedObjectContext *localContext){
    for (NSDictionary *personInfo in listOfPeople)
    {
        PersonEntity *person = [PersonEntity createInContext:localContext];
        [person setValuesForKeysWithDictionary:personInfo];
    }
} completion:^{
    self.people = [PersonEntity findAll];
}];

Source

Thus, regardless of the number of records you return, you simply receive all the content. This works for small datasets, but I want to be more efficient. I read many times so as not to read / write data by streams, so fetching after this turns into this problem, but I do not want to get everything, I just want new records.

My problem

, . API ( , - , ) , , API , .

, : , , ? API?. , :

  • , id IN array_of_ids.
  • , , x.

, , , , , ? , -

EDIT:

, , . , , . .

+4
2

NSFetchedResultsController? , , . NSFetchedResultsControllerDelegate , (, , ), .

0

, API. , enities . timestamp PersonEntity. , , . :

@property NSDate *lastViewRefreshTime;
@property NSDate *oldestEntityToDisplay;
(...)
if (self.lastViewRefreshTime.timeIntervalSinceNow < -3) {
        self.oldestEntityToDisplay = self.lastViewRefreshTime;
    }
self.lastViewRefreshTime = [NSDate date];
[self displayPersonsAddedAfter: self.oldestEntityToDisplay];

, API 3 , .

0

Source: https://habr.com/ru/post/1655623/


All Articles