I'm trying to build a simple to-do list app with Swift 3 and Xcode 8. It includes a text input box, a submit button, and a table, where each cell includes a title and a subtitle that shows the date. So far, all of this has worked.
Currently, I'm trying to get it to save when I leave the app. I've tried user defaults (which I have succesfully used in the past, but wasn't working for this), core data (which seemed like a lot of excess code, will do if I have to), and a JSON file I tried to read and write from with SwiftyJSON (have also succesfully used this). None of these have worked.
What would be the simplest/best way to solve my problem?
Thank you!
The simplest way to persist data locally in those circumstances is probably to use NSCoding. See the "Persist Data" section in Apple's Start Developing iOS Apps (Swift) guide.
You might also want to take a look at Apple's sample code Lister (for watchOS, iOS, and OS X), which demonstrates a productivity app for iOS and OS X that enables you to create and share to-do lists across devices. A user can store their documents both locally or in iCloud, but this may be more than you need for your use case.
You can save your data into: userdefaults, files in documents folder, coredata, SQLite db, third-party like realm. You can send also to your remote server.
Related
My question is fairly simple though I couslnt find anything specifically answering it till yet.
I have an obj-c app that I'm updating fairly often. I would like to create a new swift app with the same bundle id to replace the older one as an update. I have like 8 core data model versions within my old app that I would love to migrate to the new swift-from-scratch-app, so my users wont lose their data. Is it even possible??
Thank u
If you're using the exact same bundle ID (this is key), then from the perspective of iOS, it is considered exactly the same app. Consider your users having version 1.x of your app installed on their devices and you release 2.x using your new Swift project. Your user, nor iOS, will be able to tell this is a brand new app written in Swift.
The gotcha here is that unless you use the exact same model names, CoreData won't be able to be initialized with existing data and you'll be forced to write a custom migration for this.
My suggestion to you is to copy the xcdatamodel file from your old project and change the language of the code it generates once copied into the new project.
Then regenerate your entities in Swift and continue working like nothing ever happened.
Hi I struck at core data migration issue. I have developed a app which is new version of an existing app in Appstore. But i don't have source code of previous version and don't know exactly that whether core data was used in old version or not. So how to update my app (which has core data) to Appstore without any crashes. Any quick solution please?
Since you mention in a comment that you want to ignore the old data, you don't need to do any kind of migration. Model migration is about updating existing data to work with a newer data model. The only danger would be if the old app used Core Data and your new app attempts to use a persistent store file with the same file name. Then your app would attempt to load the old data, since it would find that file with the correct name.
The easiest way to test this is:
Install the existing app (from the app store)
Use this app until you're sure it's saving some data
Install the new version from Xcode. Since it's an upgrade, it will overwrite the app store copy.
By then you'll know exactly what happens when upgrading. If you get a Core Data related crash, change your Core Data setup to use a different filename. Then delete the app from your iOS device and repeat the test from above.
I created a core data model that was working fine until I added a new entity to the model, my code is fine I think... because Xcode can't detect any errors, but as soon as I run my app on the simulator the compiler gives me an error saying
reason=The model used to open the store is incompatible with the one used to create the store
I don't quite understand what this error means. I did search for references regarding this error but the one I found is outdated it was from Xcode 3.2, I'm currently running Xcode 7.1.1, interestingly enough there's no check mark on none of the replies. Despite that I still tried to use some of those methods and nothing has worked.
Things I tried
Resetting iOS Simulator -> Reset Contents and Settings...
Deleted the [Simulator App Folder]/Document/*.sqlite and ran the project
Have you had the same problem? If so can you help me troubleshoot it? Thanks.
That error means that you changed the model so that it doesn't match the one you were using when you created the app's data. It's the classic sign of changing a data model and not either doing model migration or deleting the app from the phone/simulator (and it's been pretty much the same as long as Core Data has existed, so old information is not irrelevant). Deleting the app from the simulator would normally be all that's needed to run the app again. If it's not working, then you're somehow leaving old data in place.
The best way to delete an app from the simulator is usually the same as you'd delete an app on a real device-- click and hold until the app icon starts moving, then click the "x" on its icon to delete it.
If deleting the app doesn't help, you're using old data some other way. Maybe you have some pre-loaded data built into the app that was configured using an old version of the model?
My favorite solution is to open the sqlite database with a third party app, then open the Z_MetaData table and delete the row found in there.
Works like a charm every time.
It may affect your 'migration' if you do migrations.
We do not, so not an issue.
I have a swift iPhone application that stored data the user entered into various variables, structs, objects, etc. I would like to write that data to a file. I have a template that will be stored in my project files on the phone. So what the app is supposed to do is take that template, save it as another file, and add the data the user entered from the app to the file.
My question is, I already have it set up so the user enters the data into the app using swift. Is it possible to integrate C so that the C file takes the information from Swift file and writes that data to a file? If so, can you point me to a direction? (tutorial if possible..)
I did some research on it and sadly the only thing I could find is obj-c stuff with view controllers and how to integrate obj-c with swift.
Thank you!
Yes! You can mix them. This is a link to download e-book "Using Swift with Cocoa and Objective-C"
It's FREE.
https://itunes.apple.com/it/book/using-swift-cocoa-objective/id888894773?mt=11
I have a problem. I need to make the synchronization between the two devices
At first I was trying to set up synchronization through СoreData. But it's not a very good work. Adding new entities worked fine. But the editing of previously created work is not good.
I looked as done in other applications. And I saw that in the majority of synchronization is implemented through .entry file . What is this format is that? I tried to find information about it, and how to create it. But I could not find it. Please tell me how is synchronized through .entry . Maybe there are some tutorials.
Thank you for your time
Added Image Example this files in iCloude. It's file created one well-known application
These .entry files are not publicly part of any iCloud API, so no information is available on them. In fact if I Google for "iCloud .entry", this question is currently the #1 result (and the only one in the top 10 that is even remotely relevant).
Since these apps appear to be using iCloud, and since the file in question is a PNG, it's very likely that the apps are using iCloud file synchronization and that the .entry files are an undocumented internal implementation detail. They aren't something you create or use directly, but are (probably) something that iCloud uses for bookkeeping when you're using the file sync API.
To use this API, the basic steps are:
Create the file locally
Use NSFileManager to move the local file to iCloud
Use NSMetadataQuery to find files in iCloud that haven't been downloaded yet
Use NSFileManager to download iCloud files to the local device.
Apple provides a lot of documentation on this process, plus WWDC videos and sample code.
This is nothing like Core Data syncing. You can't sync a Core Data persistent store file this way-- Apple specifically warns that it's likely to get corrupted. But you can use it for most other types of documents.