how to use sqlite db created from core data after repopulating it on the server side? - iphone

I created sqlite db from core data and i want to repopulate it on the server side and download it again to device with overwriting existing sqlite db. Is it possible to repopulate on server side and is it work if overwrite with existing db?

As far as i know, if you are saving data through core data, it will create usual sqlite db, but all tables, field names will has a Z_ prefix.
You can simply use this db on the server side, that download it as file and overwrite exiting db on device.
Sorry for my broken english if something wrong.

Do not ever mess with a Core Data db outside of the application. In fact, forget it is a db because it is simply a storage mechanism.
What you need to do is to send the objects to the server (xml is probably the easiest), manipulate the data on the server, send the data back to the app, import the data into Core Data.

Related

Where is my table in the sqlite file referenced in NSPersistentCloudKitContainer in AppDelegate?

I am new to sqlite. I successfully implemented CRUD methods to CloudKit in my app. I am not using Core Data. I read in the Apple developer documentation on Remote Records that:
CloudKit stores your records in iCloud and uses subscriptions to notify your app in real time about record changes. You then use change tokens to handle these changes efficiently. Additionally, you can improve your app’s performance and support offline use by storing records in a local cache.
I am interested in this local cache. After some digging around, it looks like this cache is automatically created and stored in the container referenced in AppDelegate.swift. I put a breakpoint in my code, and in the Debug Console, I typed: po (UIApplication.shared.delegate as! AppDelegate).persistentContainer
to discover where the .sqlite file is located. It shows:
<NSPersistentCloudKitContainer: 0x600002934c40>
CoreData: debug: CoreData+CloudKit: -NSCloudKitMirroringDelegate observeChangesForStore:inPersistentStoreCoordinator:: <NSCloudKitMirroringDelegate: 0x6000007e9d40>: Observing store: <NSSQLCore: 0x7f8276e1cc70> (URL: file:///Users/xxxxxxxx/Library/Developer/CoreSimulator/Devices/30CE52B9-E246-4B40-82F4-13DBE48EB7D6/data/Containers/Data/Application/5E26076E-12F0-4E86-9B1D-BED642C01BB6/Library/Application%20Support/MyAppName.sqlite)
In a Terminal window, I typed:
open file:///Users/xxxxxxxx/Library/Developer/CoreSimulator/Devices/30CE52B9-E246-4B40-82F4-13DBE48EB7D6/data/Containers/Data/Application/5E26076E-12F0-4E86-9B1D-BED642C01BB6/Library/Application%20Support/MyAppName.sqlite
It opened the .sqlite file in Liya. I see a bunch of tables like these:
.
I inspected each table in the list. None of the table names match the name of the Record Type and none of the field names match the names of the Record Fields that I see in the CloudKit Dashboard. So, is this .sqlite file really the cache that CloudKit created locally to "mirror" the database in iCloud? If so, how do I make use of this local cache when I can't even recognize the table name or field names used in my app?
The database you are viewing is, in fact, Core Data. When you use NSPersistentCloudKitContainer with CloudKit, your app will use Core Data as the local cache.
The SQLite database that Core Data uses is not meant to be manipulated directly since the Core Data framework manages how and when data is transacted with SQLite. You should only interface with the Core Data framework in your app and not worry about what is happening under the hood in SQLite.
If you want raw access to SQLite and full control over how your data is named and structured, then you will need to do things manually. You will need to setup and manage your own SQLite transactions and then manually manage how you sync with CloudKit.
Here are a couple good options for SQLite frameworks:
SQLite.swift: https://github.com/stephencelis/SQLite.swift
GRDB: https://github.com/groue/GRDB.swift

Getting SQLite from remote and store locally in Flutter

Im setting up an app, a sort of local guide; my idea is to have the app to work even offline, by storing most of its content locally in a SQLite database.
Since i already have the content in a database, i'd like to retrive that database from my server and store it; i already know how to get data from an API and save it in the local database, but i think getting the db file from remote and cloning in the app its lesse labour intensive.
At the moment running the app the first time i can create the empty database, and save the date of the action, i can also GET from http the database, but i dont know where to save it to have my local database use this data (to be honest i don't even know if it is possible), also i would like to periodically check on remote to see if the data was updated and get a fresh db to override the one present.
Anyone know if and where to save the db content from remote?
Thank you

How to have core data restore the DB after deletion

We have a process in our application that upon changing users we delete the contents of the documents directory. One of the casualties is our core data sqlite database file and support.
To the question, does anyone know of a way to tell core data to replace that file in an empty state?

iOS Programming suggestion request REF sqlite/iOS app design

I currently have an iOS app that provides a sqlite DB for the data backend to the app. This data is basically a list of information. Within this app I allow the users to mark records as bookmarked (sets a value in the DB). The problem here is when I post updates to the app via the internet (update through software) for data changes, the new downloaded DB wipes out the old one thereby removing the user customizations.
Any ideas on an easy method to search the current sqlite DB for those changes, store them temporarily, then import the new DB and transfer the changes to the new DB? Could I for instance use a Core Data element at the same time as using the sqlite DB backend? Maybe a key/value pair system?
Any suggestions would be greatly appreciated.
Thanks
-LK
The way I've done it is this:
For your new version, rename your .sqlite file e.g. foo-v2.sqlite. Then, during initialization, check to see if the sqlite file for the old version is there - if so, copy over the necessary info and then delete the old sqlite file.

iphone data storage to remote SQL database server

I have a task at hand to create a iphone app which is required to do the following.
the app should get the data entered by the user and store it into a remote server database.
other app user can see the data in the remote database.
store login information of the user using the app so as to keep track of what information as uploaded and by who
the thing that i would like to know.
1) What SQL server database is best to accomplish the task.
2) what format is best to retrieve the information from the database.
3) how to send the data from the iphone to the remover server database to it can store the data.
i read up on SQLite and found out that this particular database is a offline which stores the data locally so it cannot be viewed by other used. i wan to use a SQL database which can be accessed remotely.
1) What SQL server database is best to accomplish the task.
For implementation on server side, you can implement any server database, it does not matter. For implementation on iphone device, you have to implement SQLite database, which will provide storage locally.
2) what format is best to retrieve the information from the database.
You can retrieve data using XML or JSON format which will be parsed in device and can be stored in SQLite database, it is the easiest way to transfer data between client and server.
3) how to send the data from the iphone to the remover server database to it can store the data.
You can send data from iphone to server in XML/JSON format or by passing parameters in POST or GET request method then this format is parsed on server side and store data on server database.
For all this, you will require to implement API on server side, which will be the interface between server and device.
I know you mention SQL specifically, but is this a requirement or just a choice based on what most other people are doing? I ask because personally I would give serious thought to using a NoSQL database (document store) like couchdb for such a task. Deploying couch means you often don't need server side application layer at all, the way you will for a SQL based solution.
You talk to couch using HTTP which is perfectly suited to the ASIHTTPRequest library for example. Fetching data is usually a GET request and storing documents is done with PUT. All the data in or out comes back as JSON so a good JSON library will make life easier.
The combination of couchdb and the 2 libraries linked above, makes developing a data driven application really, really easy.
That's how I would do it ...
If you really need to stick with SQL, then as Jignesh says, use whatever database you like and implement a suitable API in the server side language of your choice. You can transfer the data however you like although JSON would still get my vote as a relatively light protocol that remains human readable.
You can use a cloud-enabled database service, like windows azure.