Working With CoreData - ios

I want to store only 20 Notifications which include
"Title,Detail and Received Time" fields inside Notifications Table.But,I only found solution for working CoreData with CRUD some example.But,I am still seeking for working with limitation when user fetch new results from api inside device database.That is where I am stuck.I don't know how to do it using CoreData because I was beginner at that.
Requirement :
1.Application can only store 20 Records maximum.So,when it reach max length,it will do First-In-Last-Out to Notifications table base on Notifications Received Time.(First Problem)
2.Every time the user do pull to refresh,my app fetch new notification from Web-Backend and replace or overwrite on user device database when it successfully download new notifications like "Push Notification Service App" on App Store.(Second Problem)
I really need a hand to pick up with core data flow which I am stucking at things I am not familiar with.
Any help or guide please?(Posting Sample with .json is appreciated)

use NSUserDefaults for that if your data size is small

Consider using a service, like RestKit or similar, to support your web service interaction. Each record should have a unique identifier supplied by the server so you can find an existing copy and update it instead of needing to delete everything and recreated it.
To trim your data use a fetch request with a fetchOffset and a sortDescriptors by date. This allows you to skip the newest 20 items and gives you a list of everything that needs to be deleted. If you're using RestKit you can supply this fetch request in a fetch request block and the deletion will be done for you.

Related

Query Changes after receiving CKDatabaseNotification

I'm currently building an app that allows user to share events and check in their guests simultaneously using multiple phones. I managed to set up CKQuerySubscription and update, delete and create works fine but only on the primary phone (the one sharing the event).
I recently found out that for a non-primary user to get notifications, it has to get notifications from CKDatabaseNotification which i set up and it works as I am getting remote notifications when I make changes through CloudKit Dashboard.
But the notification i get (CKDatabaseNotification) does not come with anything that would allow me to find what records changed. I've tried casting it as CKNotification as suggested on this link but as expected it fails.
I have a custom zone set up and my questions are as below:
How do I get any information about what changed from a CKDatabaseNotification?
Am I even doing that the right way? I've read somewhere else as well that some people managed to set up subscription through CKQuerySubscription on a shared database as long as it is on a custom zone, which I have but my codes told me subscription failed.
The CKDatabaseNotification will only tell you that something has changed, not what it is. The recommended path forward is you use a CKFetchDatabaseChangesOperation to find out what record zones have changes. Then you use the record zone IDs from that operation in a CKFetchRecordZoneChangesOperation to get all the changes.
There's a bit more information in the CloudKit Quick Start Guide
I'm cherry picking some relevant info below:
After app launch or the receipt of a push, your app uses CKFetchDatabaseChangesOperation and then CKFetchRecordZoneChangesOperation to ask the server for only the changes since the last time it updated.
The key to these operations is the previousServerChangeToken object,
which tells the server when your app last spoke to the server,
allowing the server to return only the items that were changed since
that time.
First your app will use a CKFetchDatabaseChangesOperation to find out
which zones have changed
Then
Next your app uses a CKFetchRecordZoneChangesOperation object with the
set of zone IDs you just collected to do the following:
• Create and update any changed records
• Delete any records that no longer exist
• Update the zone change tokens
The WWDC video CloudKit Best Practices addresses this topic as well.

Manage iPhone app both online and offline

I am stuck to a point, I am managing my app offline also. first time i get all profile data from a webservices and store its path into sqlite, I am facing problem in updating those data, like if images are changed how should I notify into my app, I searched it on Google and only solution is to send push notification to app when record is updated. What if user do not allow push notification?
Is there any other solution to manage app offline and update only when record is changed from online database?
implement offline mode in application
put one extra column data_status in all table That indicate data is entered in local database is in offline or online mode. 1 for online and 0 for offline
call API every-time with last response date and time on some frequent time interval(if required to update data frequent otherwise call when need) and API respond only changed or new data
when application is online
call API and Store data in local data base with data_status = 1
and then display that data on screen from local database
now when call again same API with server API response time and date and API respond only changed or new data.
when application is offline
check app is offline then skip API calling function and fetch data from local database.
now, when action perform on data like update insert. make that row data_status = 0
when internet come check local data base which data is with data_status = 0 and call web service according that one by one, and getting success of each API make data_status = 1 and update row in local database.
Thank you
I would suggest to give Image Name base on timestamp and unique userId. like as below.
ImageName = yourUserId_timestamp.(111_1234567).
So Image name always be unique. so when you fetch date from server. first check that image is already exist in data base or not. if it is not then update image name with help of userId and store it. if it is then leave it.
Thanks
Nothing to change from your (application) side, your server guy can handle this flow. Whenever a new image will be upload, a new URL will be generated that you will for particular object. You can replace it in your local (simply update existing data).
You can use API's to check version of data cached in App. If updated version is available on server then you have to update local data with updated server data.

How to handle the address book in my application in an effective way

I am creating an app which using address book.I am sending all my contacts to server each time to get the list of contacts who all are using the app and I should get the notification for new users in my contacts.I don’t think it is an effective way(sending all contacts everytime).Can any one suggest me how to handle the scenarios like edit contact,delete contact.How to avoid the sending of all contacts to server each time when I am opening the app.Also suggest me an effective algorithm to make best app
You could just send the changed contact to the server, use an additional field like, status and save this status from the last time you sync to server, if a contact created, or edited or deleted, you just need to change this field to not sync, and when you sending data to the server, send this not synced data to the server.

How to update an ios App with current information daily

I'm trying to design an app for my club at school. What I want to happen is when there is new information about meetings or events I want to be able to add this new content to the app and then have it visible by other app users.
So my question is how would I approach this idea?
Conceptually, you're going to need to have a centralized server which contains the data, an API to connect an application to this data, and finally the end user application.
On the server end, you could have a simple database which will house the event and meeting information.
On the API end, you could have a simple script which fetches the latest entries in the database and displays the data in a standard format such as JSON or XML.
On the app end, every time the user opens the application, fetch the latest data from your API and parse it into an array. Then, just populate a table or collection with the data in that array. You could also add a pull to refresh control to fetch the latest information at any time so the user doesn't have to launch the app again.

How do I cache Firebase data for offline usage?

I build apps that bundle up JSON data. I want to switch to Firebase as my backend, but I need to ensure I can access the data even if firebase is offline. There's no guarantee that the user will have an internet connection at the time they launch the app. The data consists of a fairly large JSON blob.
I heard that firebase does cache data on iOS for offline access, and that's great. I just need to know how to bundle the data for that first time the app is ran, so the user can use the app prior to getting to a network connection.
As it sounds like you discovered, the Firebase Obj-C client does have beta support for offline access / disk persistence. Details can be found here.
But that doesn't address your desire to "seed" the app with initial data so that it has data available before the app has ever been able to connect to Firebase. Unfortunately, there's no direct support for that.
One hacky solution you could attempt with Firebase is to just do a setValue with the data in question, in order to seed the cache. This should work but will eventually try to write that data to Firebase, when the app gets connected, so you'd probably want to have security rules to prevent the user from actually modifying that data. As I said, it'd be a hacky solution.
For now it might be best to just handle this with special logic in your app that pulls data from some other data source (hardcoded values or an embedded file or whatever) until the first time you get data from Firebase.
Sorry there's no direct support for this at the moment. We'll take a look to see if we could support this more directly in the future.

Resources