I cant seem to retrieve any information from my firebase database. I created a table with both author and title available.
This is the code i ran in my ios app, but the app just keeps crashing.
// Get a reference to our posts
Firebase *ref = [[Firebase alloc] initWithUrl: #"**********"];
[ref observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
NSLog(#"%#", snapshot.value[#"author"]);
NSLog(#"%#", snapshot.value[#"title"]);
}];
}
I cant tell whats going wrong, any help with this i will be appreciated.
Thank you
change event type to
[ref observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
NSLog(#"%#", snapshot.value[#"author"]);
NSLog(#"%#", snapshot.value[#"title"]);
}];
You would generally not store data like that right under your root node - it would generally be a child node(s) stored under the root node, like this:
sizzling-inferno-255
book_node_0
author: test_author
title: test title
book_node_1
author: another author
title: another title title
Firebase *ref = [[Firebase alloc] initWithUrl: #"sizzling-inferno-255"];
[ref observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
NSLog(#"%#", snapshot.value[#"author"]);
NSLog(#"%#", snapshot.value[#"title"]);
}];
will iterate over each book node and return a snapshot of that node. So the first time a snapshot is returned, it will be
book_node_0
author: test_author
title: test title
and the second time a snapshot is returned will be
book_node_1
author: another author
title: another title title
the book_node keys are generated with childByAutoId.
ChildAdded reads in each child node one at a time as a snapshot.
Value reads in everything in the node; all child nodes, those children etc and can be a large amount of data. If the ref node contains children, like in this answer, all of the nodes are returned and they need to be iterated over to get the child data, so within the observe block...
for child in snapshot.children {
NSLog(#"%#", child.value[#"author"]);
}
Edit - and to specifically answer the OP question, here's how you do it with the Firebase structure in the question:
self.myRootRef = [[Firebase alloc] initWithUrl:#"https://**********"];
[ref observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
NSString *key = snapshot.key;
NSString *value = snapshot.value;
if ( [key isEqualToString:#"author"] ) {
NSLog(#"author = %#", value);
} else if ([snapshot.key isEqualToString:#"title"] ) {
NSLog(#"title = %#", value);
}
}];
Related
At the moment I have an app that is using Firebase database to store the users:
Name
School
Subject
Within three separate textfields.
Right now I've managed to allow the user to enter his details within these three text fields and hit a button labeled update, which then sends the data to my firebase console.
This means that the user can enter their details, hit update and see them while they are logged in. however as soon as they logout and login again the textfields are blank, despite the fact that firebase has the info stored on its server.
I've been playing around all day trying to get it to work but to no avail.
Here we have the firebase reference and auth:
self.rootRef = [[FIRDatabase database] reference];
self.user = [FIRAuth auth].currentUser;
And the update button code that sends the users info to the firebase database:
- (IBAction)didTapEditProfile:(id)sender {
if (![_textFieldOne.text isEqual: #""]) {
NSString *item = _textFieldOne.text;
[[[[_rootRef child:#"users"] child:_user.uid] child:#"Name"] setValue:item];
}
if (![_textFieldTwo.text isEqual: #""]) {
NSString *itemTwo = _textFieldTwo.text;
[[[[_rootRef child:#"users"] child:_user.uid] child:#"School"] setValue:itemTwo];
}
if (![_textFieldThree.text isEqual: #""]) {
NSString *itemThree = _textFieldThree.text;
[[[[_rootRef child:#"users"] child:_user.uid] child:#"Subject"] setValue:itemThree];
}
}
The problem i'm having is with retrieving the data within the code below which is situated in the viewDidAppear:
[[_rootRef child:#"users"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
NSDictionary *usersDict = snapshot.value;
NSLog(#"%#",usersDict);
[usersDict objectForKey:_user.uid];
//I've tried numerous solutions here to get the text fields to display the info from the database
mainly:
NSString *field;
field = _textFieldOne.text;
field = [userDict objectForKey #"Name"];
The code i'm attempting to translate from looks like this:
field(userDict?.objectForKey("Name") as? String)
}];
Any ideas?
I managed to get everything sorted as follows:
-(void)configure:(NSString *)field {
_textFieldOne.text = field;
}
and then within the code I was having the problem:
[[_rootRef child:#"users"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
NSDictionary *usersDict = snapshot.value;
NSLog(#"%#",usersDict);
[usersDict objectForKey:_user.uid];
[self configure:[userDict objectForKey:#"Name"]];
}];
Thanks for the help.
Hey Joshua I have implemented the firebase and successfully retrieve the data from the firebase but the problem is i have implemented that in swift... here is my actual code if this helps you......
**rootRef.queryOrderedByChild("search what you want like specific name etc etc").observeEventType(.ChildAdded, withBlock: { snapshot in
if snapshot.value is NSNull {
print("the chat model is null")
SVProgressHUD.dismiss()
SVProgressHUD.showErrorWithStatus("No messages")
}
else {
print(snapshot) }**
Or if you want to fetch the whole database JSON here is the query
**rootref.observeEventType(.ChildAdded) { (snapshot: FDataSnapshot!) in
// 3
let id = snapshot.value["senderId"] as! String
let text = snapshot.value["text"] as! String
// 4
self.addMessage(id, text: text)
// 5
self.finishReceivingMessage()
}**
Here is a nice tutorial on this
https://www.raywenderlich.com/122148/firebase-tutorial-real-time-chat
If you want this to convert in objective-c then of course I will help you
This question already has an answer here:
Querying for existing nodes in Firebase is returning null
(1 answer)
Closed 6 years ago.
I am trying to understand how i can do a search in my Firebase database to check if a record exists and if so to grab details under that specific branch.
Currently i can accomplish what i need to by grabbing a specific record and placing it into a textfield and going from there, however i need to do a check on multiple records in the database till a specific record is found and from there load all the details under that branch.
Heres what i am doing currently;
Firebase *ref = [[Firebase alloc] initWithUrl: #"https://sizzling-inferno-255.firebaseio.com/"];
//Refer to users entity
Firebase *usersRef = [ref childByAppendingPath: #"id/"];
// Read data and react to changes
[usersRef observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
NSLog(#"%# -> %#", snapshot.key, snapshot.value);
NSDictionary *dict=[NSDictionary dictionaryWithDictionary:snapshot.value];
NSString *stri=[NSString stringWithFormat:#"%#",dict];
_textview1.text = stri;
string1 = snapshot.key;
_label1.text = string1;
}];
Feel free to check for yourself using the URL, i only use this for testing anyway.
I found something close to what i need however it is not in Objective C.
I'm new in firebase, but I've been working too much on the new version.
Is Record the name of your key inside of sizzling-inferno-255? So if it is. In this new version you can retrieve some info by:
FIRDatabaseReference *usersRef = [[FIRDatabase database] reference];
[[[[usersRef child:#"results"] child:#"sizzleing-inferno-255"] child:#"record"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
NSString *record = snapshot.value;
if(record.length > 0) {
// Yes! there is a record.
}
}
Is that what you want? Let me know.
I've read that it's important to keep data flatter for Firebase and to also only nest data that you intend to call. I've done those things, but Firebase is still too slow at retrieving data. Here's an example:
My data looks like this:
--English
----Ari : 4
----Philip : 2
----John : 6
And my code looks like this:
[super viewDidLoad];
[[DataSource sharedInstance].selectedLanguageMutableArray removeAllObjects];
//Retrieving Data From Firebase
NSString* selectedLanguagePath = [NSString stringWithFormat:#"languages/%#", [DataSource sharedInstance].languageSelected];
Firebase *languagesRef = [[DataSource sharedInstance].ref childByAppendingPath:selectedLanguagePath];
[[languagesRef queryOrderedByValue] observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
[self.distanceMutableArray addObject:snapshot.key];
NSLog(#"%#", snapshot.key);
NSLog(#"%#", snapshot.value);
NSLog(#"%#", self.distanceMutableArray);
}];
//Selected Languages Mutable Array
[[DataSource sharedInstance].selectedLanguageMutableArray removeAllObjects];
for (NSInteger i = 0; i < self.distanceMutableArray.count; i++) {
UserCustomizationData *item = [[UserCustomizationData alloc] init];
NSString* selectedUser = self.distanceMutableArray[i];
Firebase* selectedUserRef = [[DataSource sharedInstance].usersRef childByAppendingPath:selectedUser];
if (selectedUser.length > 0) {
Firebase* profilePicRef = [selectedUserRef childByAppendingPath:#"profilePicture"];
[profilePicRef observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
NSString* profPicString = snapshot.value;
NSData *dataFromBase64=[NSData base64DataFromString:profPicString];
UIImage *profPicImage = [[UIImage alloc]initWithData:dataFromBase64];
item.profilePicture = profPicImage;
}];
[[DataSource sharedInstance].selectedLanguageMutableArray addObject:item];
}
}
However, the for loop runs before the self.distanceMutableArray can populate. This throws everything off because the for loop relies on the self.distanceMutableArray being populated.
Is there a way to retrieve data such that the code will run fluidly and in the order that it is written?
The issue here is that Firebase works via asynchronous calls; your code will not work consistently because the code below the Firebase block may be called before the block completes.
You will need to start coding asynchronously and only perform actions on snapshot data after you know for sure it has been populated (inside the block)
[[languagesRef queryOrderedByValue] observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
//at this point, firebase has loaded the snapshot
// so its available to work with
[self.distanceMutableArray addObject:snapshot.key];
for (NSInteger i = 0; i < self.distanceMutableArray.count; i++) {
//do some stuff with the items in snapshot
}
}];
//don't work with anything that was from the snapshot as it may have not been filled yet
However there's an issue as the code is using childAdded, so that will iterate over each item in the firebase node, so that code won't work either as it won't load the array correctly (yes we can fix that by populating the array during each loop).
The additional challenge here is that you need to retrieve data from Firebase based on the result of your first snapshot. Again, same situation exists; only perform actions on that retrieved data after you know for sure it has been retrieved.
One solution is to load in the entire dataset at one time and iterate over it (by value instead of added). If your data sets are smaller that works. However, for big datasets that can be too much.
[[languagesRef queryOrderedByValue] observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
//at this point, firebase has loaded the snapshot
// so its available to work with and loaded with
//everything in the node
for ( FDataSnapshot *child in snapshot.children) {
NSDictionary *dict = child.value;
NSString *uid = child.key;
[self.distanceMutableArray addObject:uid];
}
// now the array is loaded do something with it
}];
Another option is to change how your data is stored in firebase so you can retrieve data together so you dont have to make multiple observe calls.
When I create a new value on Firebase does it automatically create a createdAt and updatedAt field or am I supposed to handle that on my own? I've noticed there's a constants kFirebaseServerValueTimestamp which is essentially a dictionary. How can I use that to retrieve that value after creating my object?
You can get the value by listening to the location:
Firebase *ref = [[Firebase alloc] initWithUrl:#"<my-firebase-app>/timestamp"];
// listen for update
[ref observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
NSLog(#"%#", snapshot.value);
}];
// save timestamp
[ref setValue: kFirebaseServerValueTimestamp];
// Posts a message to the backend database
- (void)postMessage:(NSString *)message
{
// return right away if message is nil or all whitespace
if (!message || ![[message stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]) {
return;
}
// Create new Message object and set relationships
PFObject *postedMessage = [PFObject objectWithClassName:#"Message"];
postedMessage[#"text"] = message;
PFRelation *tagRelation = [postedMessage relationForKey:#"tags"];
NSMutableArray *tags = [self generateTagsFromMessage:message];
NSLog(#"Number of Tags Found: %lu", (unsigned long)[tags count]);
[PFObject saveAllInBackground:tags block:^(BOOL succeeded, NSError *error) {
for (PFObject *tag in tags) {
// [tagRelation addObject:tag];
NSLog(#"%#", tag[#"text"]);
[tag setObject:postedMessage forKey:#"message"];
}
}];
[postedMessage saveInBackground]
}
I am using Parse as the back end for my application, but am having inexplicable difficulty creating a one-to-many relationship.
The idea is to make it so that users can post messages to the database with a tag on them, and the database stores Messages as one class, and Tags as another. The Message class has a "tags" field intended to store all tags in the message, and the Tag class has a "message" field intended to store the parent message of the tag. I thought I followed the tutorial of how to create a One-to-many relationship in Parse very well, but no matter what I cannot see the relationship on the Data Browser in Parse.
I have tried getting the relation to work by both simply setting the postedMessage as the newTag's message field, and also by creating a PFRelation * for the tags field of the message and adding tags to that. None of it works.
Does anybody know what my problem might be?
Thank you!
Edit
Specifically, the problem is that no relation is formed between the objects. By inspecting the data browser I can see that both the "tags" relational field of Message and "message" relational field of Tag remain blank after running this code.
An issue with your above code is that you're creating two new objects at once and triggering background saves on both at once.
For this to work, you need to save one side of the relationship first, then create the new objects, relate them and save them (in the block).
In your case you have a single message from multiple tags, so create the message and save it, then in the save block create the tags, add them to the message and re-save the message (it'll walk the tree looking for changes and save the new tags).
// Create new Message object
PFObject *postedMessage = [PFObject objectWithClassName:#"Message"];
postedMessage[#"text"] = message;
[postedMessage saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
// message saved, now create tags with link to message
PFRelation *tagRelation = [postedMessage relationForKey:#"tags"];
NSMutableArray *tags = [self generateTagsFromMessage:message];
NSLog(#"Number of Tags Found: %lu", (unsigned long)[tags count]);
for (PFObject *tag in tags) {
NSLog(#"%#", tag[#"text"]);
[tag setObject:postedMessage forKey:#"message"];
}
// now save the tags
[PFObject saveAllInBackground:tags block:^(BOOL succeeded, NSError *error) {
// now add the relationships
for (PFObject *tag in tags) {
[tagRelation addObject:tag];
}
// and save
[postedMessage saveInBackground];
}];
}];
Your code is almost correct, but you missed a few details. See my working version of your code re-written below. But first, read through the examples from Parse.
Take a look at this example from the Parse iOS documentation where they are setting the pointer relationship between two objects; a One-to-One relationship.
Neither object must have an objectId before creating the relationship.
// Create the post
PFObject *myPost = [PFObject objectWithClassName:#"Post"];
myPost[#"title"] = #"I'm Hungry";
myPost[#"content"] = #"Where should we go for lunch?";
// Create the comment
PFObject *myComment = [PFObject objectWithClassName:#"Comment"];
myComment[#"content"] = #"Let's do Sushirrito.";
// Add a relation between the Post and Comment
myComment[#"parent"] = myPost;
// This will save both myPost and myComment
[myComment saveInBackground];
What you are looking for is either a One-to-Many relationship array of pointers, which is outlined here and copied below for easy reference.
One object must have an objectId before creating the relationship
// Create Post
PFObject *newPost = [PFObject objectWithClassName:#"Post"];
// Set text content
[newPost setObject:[textView text] forKey:#"textContent"];
// Create relationship
[newPost setObject:[PFUser currentUser] forKey:#"author"];
// Save the new post
[newPost saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
// Dismiss the NewPostViewController and show the BlogTableViewController
[self dismissModalViewControllerAnimated:YES];
}
}];
OR you are looking for a One-to-Many relationship using the PFRelation object because; "you don't need to download all the Objects in a relation at once. This allows PFRelation to scale to many more objects than the NSArray of PFObject approach" . If this approach is what you are looking for, your re-written code below will work.
One object must have on objectId before creating the relationship
// Create new Message object and set relationships
PFObject *postedMessage = [PFObject objectWithClassName:#"TestClass"];
postedMessage[#"text"] = #"Hello World";
//example tags into array
PFObject *tag1 = [PFObject objectWithClassName:#"Tag"];
PFObject *tag2 = [PFObject objectWithClassName:#"Tag"];
PFObject *tag3 = [PFObject objectWithClassName:#"Tag"];
PFObject *tag4 = [PFObject objectWithClassName:#"Tag"];
NSArray *tags = [NSArray arrayWithObjects:tag1, tag2, tag3, tag4, nil];
//iterate the tags, add pointer for each
for (PFObject *tag in tags) {
//create a new tag-to-message pointer
[tag setObject:postedMessage forKey:#"message"];
}
//save all the tags and save the postedMessage
[PFObject saveAllInBackground:tags block:^(BOOL succeeded, NSError *error) {
if (!error) {
NSLog(#"created new postedMessage w/o relations :%#", postedMessage);
NSLog(#"saved tags with pointers :%#", tags);
PFRelation *tagRelation = [postedMessage relationForKey:#"tags"];
//iterate the tags, add relation for each
for (PFObject *tag in tags) {
//create a new postedMessage-to-tag relationship
[tagRelation addObject:tag];
}
//update the postedMessage
[postedMessage saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
NSLog(#"updated postedMessage with relations :%#", postedMessage);
}
}];
}
}];
Edit: Adding the code I wrote; which works as you would expect. Once you get your relationships working there is one thing you will encounter using the Browser and that is an "object not found" error when you click on an object relationship from an object that is not directly related. A better way to explain that is to refer you to my example below.
I created a Message-to-Notification-to-User relationship -- think of it in any direction you prefer. When I access this relationship data using the Browser in CoreData and I click on the Users notifications relations column, I am directed to the users notifications. However; when I click on any of the message relations column, I received the, "object not found" error message. This is expected behavior and logically it makes sense. The User object doesn't care what messages its notifications contain, much like how the messages don't care who the user is. When the User object calls delete on a notification, auto-cleanup removes the messages without the User object needing to worry about them. When an object has no reference pointers and auto-cleanup is called, the object will be removed.
- (void)addNotificationWithData:(NSDictionary*)userInfo withCompletionHandler:(void(^)(BOOL isFinished))completionHandler {
PFObject *notification = [PFObject objectWithClassName:#"Notification"];
PFObject *msg = [PFObject objectWithClassName:#"Message"];
[msg saveEventually:^(BOOL succeeded, NSError *error) {
if (!error) {
PFRelation *notification_to_message = [notification relationForKey:#"messages"];
[notification_to_message addObject:msg];
[notification saveEventually:^(BOOL succeeded, NSError *error) {
if (!error) {
PFRelation *user_to_notification = [[PFUser currentUser]relationForKey:#"notifications"];
[user_to_notification addObject:notification];
[[PFUser currentUser]saveEventually:^(BOOL succeeded, NSError *error) {
if (!error) {
//add the notication to the notification(s)
[notifications addObject:notification];
NSLog(#"the current notifications are :%#", notifications);
//call the completion handler
if (completionHandler) {
completionHandler(YES);
}
}
}];
}
}];
}
}];
}
You've commented out the addObject call to add the object to the relation. You'll need to uncomment that line in order to actually add the object to the relation.