My iOS App call a API with Restful webservice API. Then I used data and saved Json data on my disk.
On next time open app, I want to check API, if having update, the app will call and be reveice data, if not update I will use data on disk.
How to check API update? Thankyou.
If you have influence on the API then you can make a call that returns for example a timestamp of latest update. If it is newer than the one you have on your device then download the new data. Because you probably want to avoid downloading the (same) data twice. Is that what you were asking?
You can use this method in AppDelegate, which will be called when app come back to foreground. Call the API and check it's last modification date of you'r data.
func applicationWillEnterForeground(application: UIApplication) {
}
Related
I have an App written in Swift that uploads statistics to my server. My question is simple and as follows: When is the best time to upload my statistics?
One approach I came up with was that I save all the statistics locally when the app quits. And when the app opens in the future, I upload the saved statistics and clear them.
The problem is applicationWillTerminate is not called sometimes, and data may be lost without being uploaded.
So what is the best way to solve my problem?
Thanks.
Similar to #jvrmed, I recommend saving your data locally whenever you want to record a stat. But I suggest pushing that data to your server when your application is about to resign active - i.e. when it is backgrounded.
func applicationWillResignActive(_ application: UIApplication) { }
Save your data locally whenever its generated. Once the app launches, send it and clear your local cache. You can use application:didFinishLaunchingWithOptions: to detect launching.
Its a good practice to cache information that you need to keep safe periodically, instead of all at once.
I have a coaching app that has a section where I can push realtime updates out to the players like: "No Practice - Do to inclement weather, practice will be pushed until Friday"
I have been trying to figure out how to send automatic notifications when I update this UpdatesTableView with a new post. Like "New Update Posted".
I post my updates to the Firebase Database. There must be a way to listen for changes and when there is to push a notification out to all the users?
I already have firebase notifications set up in my app but I have to utilize the Firebase console to push these notifications every time i push an update. Does anyone know how to automate this? Thanks!
You can easily do that by listening/Observing to any data change at a particular location in firebase. If new child is added to that path, associated block will be called.
In your case, you can observe UpdatesTableView. and whenever you post any update, call the block which will send notification to all users.
If you are using Swift:
func observe(_ eventType: FIRDataEventType, with block: #escaping (FIRDataSnapshot) -> Void) -> UInt
If you are using Objective C:
- (FIRDatabaseHandle)
observeEventType:(FIRDataEventType)eventType
withBlock:(nonnull void (^)(FIRDataSnapshot *_Nonnull))block;
According to official firebase documentation :
observeEventType:withBlock: is used to listen for data changes at a particular location. This is the primary way to read data from the
Firebase Database. Your block will be triggered for the initial data
and again whenever the data changes.
And, Whenever you would like to stop listening to data changes, you can Use removeObserverWithHandle
Use removeObserverWithHandle: to stop receiving updates. - parameter:
eventType The type of event to listen for. - parameter: block The
block that should be called with initial data and updates. It is
passed the data as a FIRDataSnapshot. - returns: A handle used to
unregister this block later using removeObserverWithHandle:
For more and detailed information, Read iOS firebase#Attaching Observers to read data Documentation.
Also, For sending Notifications to users effieciently, you can use Firebase Notification. Have a look at it. i dont know about your usecase properly, But i think this will help.
I also stucked with same problem where I wanted to show notification to users whenever data changes in Firebase irrespective of application in foreground or background.
I achieved it by binding ChildEventListener with a service which keeps running in background. At every childAdded event data is stored in sqlited db and a new notification object is created and shown to user.
I created a saveState() method that uses UserDefaults to save certain settings at certain parts of my App. It works fine when I exit the App and return, but if I actually turn my (iOS) phone off, when I start the App again, the settings are not saved. In addition to those places where I call the saveState() method in the code, I also call saveState() in three AppDelegate functions: applicationWillResignActive, applicationDidEnterBackground and applicationWillTerminate. I have a loadState() function in viewDidLoad so any saved information will load at that time. Does anyone know what I am not doing re: saving/restoring settings when phone powered off?
When iphone off, AppDelegate's applicationWillTerminate
function Called when the application is about to terminate
how about use synchronize() after your saveState() called
In apple API https://developer.apple.com/reference/foundation/userdefaults/1414005-synchronize
Writes any modifications to the persistent domains to disk and updates all unmodified persistent domains to what is on disk.
I am using Dropbox's SDK API V1 to upload files from my iOS app. The files aren't loading in real time, but rather I push them into a queue, and they get uploaded in the background.
I can't figure out for the life of me how to take DB RestClient delegate and make it respond to a completion/failure event that happens in the background. Is that even possible?
It seems (and I am new here), that delegates are meant to respond to live events on screen. Is that even an option to assign them to anything other than a UI element/view?
Update: Adding some description of what the code looks like.
There is a lot of code and it's hard for me to tell what's relevant for this question.
I am using PHPhotoLibrary.sharedPhotoLibrary().performChanges() which has a completion handler.
On success, I send an image upload request via dispatch_async(dispatch_get_main_queue()) method.
Dropbox API has this method dropboxRestClient.uploadFile(filename, toPath: pathOnDropbox, withParentRev: nil, fromPath: localFileUrl)
In all the examples that I've seen, dropboxRestClient.delegate was assigned to a UIViewController of some sort, and never to anything else.
So I am not sure how to assign it correctly, so that it recognizes events that happen in this queue.
I am developing an application which have some pre loaded app resources that can be changed dynamically by user or admin, I have to put these app resources in document directory of my app so that it will not make me to update application on itunes.
Problem that I am facing that i have to download all the app resources from web server on local device when application launches for the first time.
I am seeking for the best method for fetching all my app resources and keep it updated? Please help me!
Actually, The problem is that I have thousands of resources like images, html and css files that have to be downloaded, and end user might feel problem in refreshing or maintain the data on server. Is there any file transfer protocol (ftp) library in iOS available to download the data and update? or any other method?
1) First time, take all the records from server along with the server date(check date parameter, if null then server passes all records i.e. first time app is launched).
2) Store that date in NSUserDefaults.
3) Second time pass that date while request, if date is not null(request is not first time).
4) Server checks the updated records(records greater than the date which you passed).
NSString *strTodaysDate = #"";
if ([[NSUserDefaults standardUserDefaults] valueForKey:#"SyncDone"] != nil)
{
//If sync all data is done first time, then pass server date else pass empty date parameter.
strTodaysDate = [NSString stringWithFormat:#"%#",[[NSUserDefaults standardUserDefaults] valueForKey:#"SyncDate"]];
} //call web service with strTodaysDate as a parameter.
You need to send simple request from application everytime application launches, which will ask server about updates.
If your content on server was updated then you need to reload your content in application.
Of course your server must know if there was some changes in content. You can track changes time on server and latest content update time in application.
You can update data of your by calling web service from application delegate from this method
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
//make call to web service and save it in your document directory
}