Equivalent of DocumentSnapshot in Firebase RealTime Database - firebase-realtime-database

DocumentSnapshot is for Firestore right? Is there any equivalent of DocumentSnapshot in Firebase RealTimeDatabase? Becasue i'll use it in my FirebaseRecyclerAdapter for recyclerview items to have an onclcick events on each items.

A DocumentSnapshot in Firestore is a snapshot of the data in a document, which is the basic unit of data storage in that database.
The equivalent on Firebase's Realtime Database would be a DataSnapshot, which contains the data for a node - the basic unit of data storage for that database. DataSnapshots are also what the FirebaseRecyclerAdapter class in the FirebaseUI library uses to track the current snapshots of the data.

Related

How to save a dictionary within a dictionary in core data

I've set up the following dictionary in my iOS app:
cardCollection = ["collectionName": collectionTitle.text, "collectionDescrip": collectionDescription.text, "numberOfCards": newCardGroup.count, "dateCreated": currentDate, "cards": [newCardGroup]]
The user creates a number individual cards, which contains text, a date and an uploaded image if the user chooses. This data is put inside a dictionary called newCardGroup. Once they're done creating a group of cards, they save this to another dictionary called cardCollection.
I've created two entities in the core data model, one with the newCardGroup attributes (called Cards) and another with the cardCollection ones (called CardCollection). The CardCollection entity is set up as the main entity with a one-to-many relationship with Cards. However, I'm having trouble with how to save this type of information using the core data model framework. I was hoping to keep it simple, and not have to use a third party storage service like Firebase, but I can't find any examples of saving a dictionary like described above in core data.

Pulling data from Dataservice Firebase

I am new to Firebase and I am trying to explore it. I already know i can make a reference to database and pull some data like: firstName, email etc. However, I have a function for uploading images to storage. I have a reference to storage but i cannot find function for storage similar to the one for database so i can pull images or their names:
DataService.instance.usersRef.observeSingleEvent(of: .value) { (snapshot: DataSnapshot) in }
Any help would be appreciated.

Firebase Swift equivalent of push()

In Javascript, calling push on a firebase ref would generate a random ID. Then you can populate the stuff below.
However, I can't find push in Swift. Do I have to generate random IDs myself?
I think you're looking for childByAutoId():
childByAutoId generates a new child location using a unique key and returns a FIRDatabaseReference to it. This is useful when the children of a Firebase Database location represent a list of items.
The unique key generated by childByAutoId: is prefixed with a client-generated timestamp so that the resulting list will be chronologically-sorted.
Declaration
func childByAutoId() -> FIRDatabaseReference
Also see: https://firebase.google.com/docs/database/ios/read-and-write#update_specific_fields

Import Data to Realm Database

I want to use a centralized database and am looking at various options to do so. From my understanding, I have three main options: SQLite, Realm, and CoreData. Are these options fine for a large centralized database for all users.
Additionally, I am trying to import data from JSON and CSV into a Realm database. Does Realm have this functionality?
As for now Realm does not seem to have the import functionality that you need. Check this thread for more information.
Realm does have a great documentation that you can read at Realm and for SQLite there is this framework (there are for sure more out there) and they both support Swift 2.x. You have to check what suits your requests most.
I can also recommend you to read this database thread at reddit.
public String composeJsonFromRealm(){
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
// Build the query looking at all users:
// Execute the query:
RealmResults<User> users = realm.where(User.class).findAll();
if (users.size()>0){
User user = users.first();
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", user.getName());
map.put("email",user.getEmail());
wordList.add(map);
}
Gson gson = new GsonBuilder().create();
return gson.toJson(wordList);
}
I am trying to import data from JSON and CSV into a Realm database. Does Realm have this functionality?
With Realm Cocoa Converter you can import CSV, XLSX, and JSON, and export CSV data. Though, at the moment, it only supports OS X.
As of now, Realm Studio does have the import functionality from CSV.
Open the Realm Studio, File -> Create Realm from -> CSV
Cheers,
Murat

UITableView handling Json and Core Data

What would be the best practise and best for user experience to achieve the following?
1:) Retrieve data from JSON
2:) Store in Core Data
3:) Display in UITableViewController
Do i store the JSON first, then populate the table using the stored data? OR Do i store it in the Core Data (background process) and populate the table using the JSON for the first time?
I want the user to be presented with a UITableview with minimum load time.
Thanks
This is what I would do:
Create your Core Data database and model.
Create a data access layer that will contain the read and write methods for each of your objects
In the read functions you can query the core data, if there is data then return that. Then in the background call the web server and and update your core data with the new JSON.
If there is no data go and request it from the web server, populate your core data tables using the JSON and then return the data from the core data so it is always consistent.
You can also have a last update date on your data so you are only requesting the new data from the web server that isnt already in your local core data DB. This will reduce the amount of data coming down to your ios device.
If you want minimum load time then I'd serve from JSON and that save to CoreData afterwards. That way the user can see content straight away without first having to wait for all the data to be saved (and parsed).
The course of action in this matter heavily depends on:
A. The amount of JSON data you are downloading
B. How effective your backend is at only sending necessary JSON results (rather than sending everything in bulk)
C. How you are attaching Core Data to your UITableViewController.
I recently made a pretty big project that does exactly this, which involved fetching a pretty big chunk of JSON, parsing it, and inserting it into Core Data. The only time there is any delay is during the initial load. This is how I accomplished it:
Download JSON. Cast as [[String: AnyObject]]: if let result = rawJSON as? [[String: AnyObject]] {}
Check to see if IDs for objects already exist in Core Data. If that object already exists, check if it needs update. If it doesn't exist, create it. Also check if IDs have been deleted from the JSON, if so remove them from.
Use NSFetchedResultsController to manage data from Core Data and populate the UITableView. I use NSFetchedResultsController rather than managedObjectContext.executeFetchRequest() because NSFetchedResultsController has delegate methods that are called every time the managedObjectContext is updated.

Resources