Swift cache (NSCache) - swift

I am using NSCache to deal with cache in my app. Can anybody explain to me how totalCostLimit and countLimit works.
I want to allow user input how may memory of using cache he wont to allow for our app and I understand that I need to use totalCostLimit and countLimit, but what happen to inputed data if user will close the app, will it be stored or cached somewhere by the app or I need to sore it in database and every time when user will open the app take that data and send it to totalCostLimit and countLimit.
Also I do not understand how to observe how much cache is weighting, I want to add that info to app so user whenever he wont will see hove much memory my app takes for cache in that specific moment.
Thank you for the answers!

Related

iPhone - keep objects in permanent memory options

I need to save my objects in permanent memory. The option I use right now is that i save my objects in the NSUserDefaults before my app quits and I retrieve them when my app starts running. This approach is not very convenient since I may lose important data in case the application crashes. Is there any way to store my objects, but when a property of an object changes, then this change is saved in the disk automatically? Except for that, there is a danger to mess the objects in ram and the objects in the disk using that architecture.
For example
-> Load objects from memory
-> [object1 setValue:#"5"]
-> Application crashes
After the crash, when the user opens the application, the value #"5" will not be available because I never saved the data.
Is there any alternative so as to make by code more safe and maintainable? CoreData is a good option for this problem , or is it an overhead?
If you need to save data, CoreData is the best. You can save after every change if you need to.
If saving info is so important you might want to figure out what could cause/is causing your application to crash and fix that. No matter how good a solution is if your app is going to randomly crash you'll probably lose some data.
It is really not the best of option to save data instantly when a value changes. It's way too expensive for app performance. You must continue using NSUserDefaults to store the values when app is force closed or entered in background.
Your app is designed by you. It MUST not crash but even if it does, don't worry. Anamolies cannot really be handled.

How to save data when app is running in background?

I am new in iOS developer. Now I am working at a background location app. I need to send data to server and save some info from feedback. First time I save the data in UIKit class, but it seems released. Second time I save the data in static variables. It still crash. Then I have to use NSUserDefaults , it works . But is this OK? Any better way? Save data to local or something else ? Thanks~
The better way is to use NSUserDefaults, you save your data there and you dont worry about the data been released or removed.
Another way is global data or singletons, but is not recommended by Apple (you can find a lot of discussions about this in the internet)
I personally recommend you to use NSUserDefaults, if you have any doubt using it we can help you.

When is it best to do an NSManagedObjectContext save?

I have noticed that for a mobile application, saving on the main thread seems to take a bit when it compares to other applications on the device. Is it recommended to only save Core Data when the application enters the background or when an application closes instead of anytime items are added and sent / received from the api?
That's kind of a broad question, but I've found that saving core data after VewDidAppear statements is better than viewWill statements. Giving the user something to engage with and persisting makes it less noticeable than on a load. However, if a user is used to waiting for something like an activity loop, adding the save to that doesn't tax it too much (IMHO).
Not sure this help, just my experience.

Can i restart an app once it becomes active again?

I have an app that stores data in a coredata db. I save data to the db from a web service when the app is started. The thing is that people can keep their app in the background even for weeks and may see data that is no longer valid. I would like to kind of reinitialize the entire app once it comes from background (a sort of restart, if you will) because i have multiple tabs. Can this be done?
You don't need to restart the app each time it enters foreground. You can disable "multitasking" by adding this to your project's info.plist
Add a new row and select “Application does not run in background” (or type “UIApplicationExitsOnSuspend”) and then toggle the checkbox.
EDIT: (Thanks to Kevin Ballard) It's better if you check if it's been a long time since the last time it downloaded the data and, in that case, re-download the data. You should only disable "multitasking" when you have a really good reason to do that. This is not that case.
Shouldn't you just refresh the UI? Core Data has plenty of notifications for keeping up with the changes in the context, for example NSManagedObjectContextObjectsDidChangeNotification which has all the updates/deletes/inserts with it as well. Just start observing it before you update the DB and you'll see if any changes occur.
No need to restart to reload the UI, all the callbacks are there...
So instead of updating your data in applicationDidFinishLaunchingWithOptions: do it in applicationDidBecomeActive: and use Core Data notifications to update the UI as necessary.

iPhone Core Data application will terminate save database required?

I have an application that allows you to edit some percentages, however it will only let you commit those changes if the percentages add up to 100. However because the Core Data template includes the save code in the application will terminate. If the user changed something and then exited the application, the item would be of course saved even though it did not add to a 100%.
Therefore I simply decided to comment out the save in the application will terminate. I know the other option would be to use another context for the edit and then merge the changes or setting my context values until the actual save point. However I do not see any harm in commenting out this line, since I save whatever I want in my application when the user clicks the save button, so my question is: is the save on the application will terminate mandatory? what possible consequences could this have?. It is important to note that the application continues to work just fine after commenting this lines (which is what I expected).
Thank you in advance.
-Oscar
You can save whenever you like.
However, you will never know when the app will terminate. Unlike applications on more conventional platforms e.g desktops, the iPhoneOS will terminate your app (from the apps perspective) at random. The only warning you will get will be the applicationWillTerminate message sent to the app delegate. If you don't handle saves there then it is highly likely that at some point, your users will lose data.
I think you should reconsider your design. It sounds like you're putting calculation into the managedobjects that could (1) be handled elsewhere in code or (2) be handled by transient properties. You shouldn't have a condition in which the managedobject can't be saved at the drop of hat. Doing so makes your datamodel utterly dependent on external code for its internal integrity. This causes problem with maintenance, portability and upgrading.
Its not mandatory to save on application will terminate. You can save when ever you feel appropriate for the context of the app.