T
T
tiger_132019-10-16 14:01:46
Swift
tiger_13, 2019-10-16 14:01:46

What is the best way to update content in the application?

I am writing a program, the database is implemented using Realm. When the application is opened for the first time (the database is empty), then I check with the usual if closure whether it is necessary to load data from the server.

if objects.count == 0 {
// загружаем данные через Alamofire request, парсим и сохраняем их в Realm
}

Essence of the question:
The data on the server is updated randomly (about once a month), how to update this data (download data from the server)?
Personally, I have three ideas:
1) each time in viewDidLoad() I make a request to the server if there are any updates, if so, then I already make a request for the data, and parse + save them
Cons: as for me, then this approach increases the load on the server + eats the RAM of the device, which is not buzzing.
2) use BGTask/BGAppRefreshTask, BGProcessingTaskRequest - IOS 13 (haven't fully smoked it yet) / application(_:performFetchWithCompletionHandler:) - IOS 7 - IOS 12.
3) send remote notification when database is updated on the server:
https://developer. apple.com/documentation/usernoti...
Cons:
- I found an article that says that if the user kills the application, then the handler will not work at all.
- misunderstanding with DeviceToken and Authorization.
Please share your experience, who has implemented this issue, and who has any ideas on how best to do it.
thank you all in advance!
PS While writing, the fourth option came - through UserDefaults, write Date there, and if more than 24 hours have passed, then ask the server if there are updates (well, write Date accordingly, and so on in a circle)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
doublench21, 2019-10-16
@tiger_13

Disclaimer: Don't reinvent the wheel!

My eyes widen from questions like this.
Well, if you have data that must be strictly up-to-date at the time the application is launched, then these are the most common requests to the server. Exactly the same as in any other application. You go into the application, the application sends a request to the server, and the server returns. You parse, then display the information. There is absolutely no difference whether something has changed there or not. You're just asking for JSON.
I can still understand if you are transferring a certain 10GB file that you don't want to download again if it hasn't changed. Here I can understand your concerns. Well, even in this case, such issues are resolved on the server by the necessary headers. One of them is E-TAG. Everything has already been thought of for you.
Returning to the author's question. If all we're talking about is just JSON compiled from a database, then don't torture yourself you know what. Just make a request and that's it. It doesn't matter if the data has changed or not. The cache will take care of everything else, both on the client (URLSession) and on the server.
-------------------------------------------------- -------------------------------------------------
When make requests to the server?
Well, depending on where this data is displayed.
  • data is global to the entire application and can be used in any controller, then:
    func application(
            _ application: UIApplication,
            willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
        ) -> Bool {
            // ...
            return true
        }

    or
    func application(
            _ application: UIApplication,
            didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
        ) -> Bool {
           // ...
            return true
        }

    or how to initialize St. in general inAppDelegate
  • if data is needed only for a specific controller, then as already written above:
    override func viewDidLoad()  {
        super.viewDidLoad()
       // ...
     }

I
Ivan Vorobei, 2019-10-16
@ivanvorobei

It's not clear what problem you are trying to solve.
To formulate a problem, you need to define :
Will update the answer after your comment.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question