I'm following a tutorial online. This tutorial covers firebase 1, however I'm currently using firebase 2
In one of the code samples he has written a reference like so
private var postRef: firebase
however when I do this within firebase 2 I get the error "Use of undeclared type 'firebase'
Now my issues is I'm trying to find the comparison between firebase 1 and 2 so I can go ahead an implement the correct code, however I've been unsuccessful in finding such information.
Can someone shed some light in regards to what the correct syntax is for firebase 2 ?
The value he assigns to this variable is a value from one of the tables, in this case its from a post table, which consists of Desc, image, Id
just change :-
private var postRef: firebase
to
private var postRef: FIRDatabaseReference!
Which you can later initialise in any scope...like:-
override func viewDidLoad(){
super.viewDidLoad()
postRef = FIRDatabase.database().reference()
}
Related
I'm using Realm Sync to store data for my iOS app, coded in Swift. I wanted to create an optional double property (budget) for a Realm object (User_budgets). I created the object in the Realm schema and then copied in the Data model SDK that Realm produces which is as below:
import Foundation
import RealmSwift
class User_budgets: EmbeddedObject {
let budget = RealmProperty<Double>()
#objc dynamic var date: Date? = nil
}
I then get the error: "Cannot find 'RealmProperty' in scope". I tried changing the code to the below:
#objc dynamic var budget: Double? = nil
But then I get the error: "Property cannot be marked #objc because its type cannot be represented in Objective-C"
I've had a search but can't seem to find anyone who's had this issue before. There's an easy work around, which is simply to make the budget property required (non-optional), but it would be good to know how to be able to create optional double properties in the future. Can anyone help me out?
I believe you're using the wrong definition for that optional as it's only available in beta 10.8.0-beta.0:
What you have is
let budget = RealmProperty<Double>()
and for all other releases it should be
let budget = RealmOptional<Double>()
See RealmProperty and RealmOptional
oh and here's a link to all of the Support Property Types
I'm using Firebase on Swift and I am trying to bypass this error:
ambiguous reference to member 'database()'
I'm following some code on youtube to get ready for my project and I see there have been changes since then and I'm not too sure if they also changed that one. Here's my code.
func name(state: String){
let ref = Database.database.reference() //This generates the error
let post : [String: AnyObject] = ["state": state as AnyObject]
ref.child("name").setValue(post)
}
Thanks
SN: I've imported Firebase and FirebaseDatabase already
Edit: Apparently, I couldn't just type it in and had to wait until it was recommended. I'll keep this question up in hopes someone will find their answer quicker than I did.
You forgot to initialize database with (), so use this:
let ref = Database.database().reference()
I have an app that was originally developed in Swift 2 / Xcode 7.3.1 using the Firebase iOS SDK v2.5.1 which is currently being updated to the latest versions of Swift/Xcode, which entails also updating the Firebase SDK.
We have hundreds of unit tests that were developed that, for the most part have been easy to update, but there are a few in particular that are tricky due to the way the old Firebase SDK was structured (and thus how it was used).
Originally, because nearly all Firebase database functionality was encompassed in one "Firebase" class, we created a custom class that inherited from the Firebase class that overrode several methods. In the latest SDK, functionality has been split further into different classes (namely the separate of Database and DatabaseReference), and most of the methods that were overridden are now a part of the DatabaseReference class in the FirebaseDatabase framework.
To make this a bit more tangible.
Old code:
class FirebaseTest: Firebase {
override func childByAutoId() {
//generate and save Auto ID internally to be used later for testing/validation
super.childByAutoID();
}
//many other overridden methods
}
//Unit tests
let fbref = FirebaseTest(url: "my-firebase.firebaseio.com/test");
// etc
The old Firebase SDK would return a Firebase reference to the base URL provided in the constructor, and because many of the properties we used were members of Firebase, all of our testing worked.
Following the SDK migration guide, I end up with the following code (simply replacing Firebase with DatabaseReference):
class FirebaseTest: DatabaseReference {
override func childByAutoId() {
//generate and save Auto ID internally to be used later for testing/validation
super.childByAutoID();
}
//many other overridden methods
}
//Unit tests
let fbref = FirebaseTest(fromUrl: "my-firebase.firebaseio.com/test")
// etc
Now, immediately, there is a problem with the above code that prevents it from compiling: the DatabaseReference constructor does not take any arguments. I can change it to the following
let fbref = FirebaseTest();
but now I know that, while it will compile, the tests will absolutely fail. The tricky part is that, in the latest SDK, database references are only supposed to be retrieved from a Database as follows:
//don't do this!
let fbref = DatabaseReference(); //will compile, but will break at runtime. Upon inspection in the debugger, there are several internal properties that should be set that are null. Those internal properties are likely all set correctly if the database reference is obtained correctly
//do it one of these ways
let fbref2 = Database.database().reference();
let fbref3 = Database.database().reference(fromUrl: "my-firebase.firebaseio.com/test") //also have access to this "reference(fromUrl: )" method from Database.database()
let fbref4 = Database.database().reference(withPath: "/test") //also have access to this "reference(withPath: )" method from Database.database()
So, hopefully my conundrum is becoming clear -- because I can only retrieve a correctly-initalized DatabaseReference from a Database, rather than my version of DatabaseReference that has overridden methods for testing, I'm fairly stuck.
Also, I have looked into creating an extension of Database itself as follows:
class FirebaseTest: DatabaseReference {
override func childByAutoId() {
//generate and save Auto ID internally to be used later for testing/validation
super.childByAutoID();
}
//many other overridden methods
}
class FirebaseDatabaseTest: Database {
override func reference(fromUrl: String) {
return FirebaseTest();
}
}
let fbref = FirebaseDatabaseTest().reference(fromUrl: "my-firebase.firebaseio.com/test")
But, even though I am intercepting the url in the overridden reference method, there's no clear way for me to "set up" the DatabaseReference the correct way using that URL.
Thoughts?
I am currently using the latest version of Parse 1.14.2 and Bolts 1.8.4.Parse is implemented correctly and I have been using it for a long time now. The problem I'm facing now is when I try to use Parse's local datastore. I have the following code in my AppDelegate.swift:
Parse.enableLocalDatastore()
Parse.setApplicationId("ID",
clientKey: "Client_Key")
I have the following code to create and save a string named firstName in a class named contact:
let contact = PFObject(className: "contact")
contact["firstName"] = "Jack"
contact.pinInBackground()
Here is the code to retrieve objects from the created class:
let query = PFQuery(className: "contact")
query.fromLocalDatastore()
query.getFirstObjectInBackgroundWithBlock({ (object, error) -> Void in
if error == nil {
if let contact = object {
print(contact.objectForKey("firstName"))
}
}
})
I have added libsqlite3.dylib to my project. My app doesn't crash when I run this code but it simply gives me the following message when I try to retrieve objects:
2016-08-29 11:31:38.049 App_Demo[14436:3504319] [Bolts] Warning: `BFTask` caught an exception in the continuation block.
This behavior is discouraged and will be removed in a future release.
Caught Exception: Method requires Pinning enabled.
Can anyone help me to work around this issue? I am guessing the issue is that this version of Bolts cannot pin Parse objects in the background and I need to work my way around this bug. Any help would be appreciated as I have been stuck at this for a while and can't find too much info online.
Edited: I have tried downgrading Bolts, but then Parse downgrades with it in Cocoapod and it causes errors in Xcode.
it's not objectforkey.
You need to call object["UsedName"] "UsedName" being the key. Hope that helps.
I am following Google's instructions on setting up Firebase https://www.firebase.com/docs/ios/quickstart.html
I suspect the problem may be because the help is for the legacy version. However deprecated should not read an error, at least I have found this for apple deprecations. The following code:
import UIKit
import Firebase
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var myRootRef = Firebase(url:"https://<YOUR-FIREBASE-APP>.firebaseio.com")
// Write data to Firebase
myRootRef.setValue("Do you have data? You'll love Firebase.")
// Do any additional setup after loading the view, typically from a nib.
}
gives the following error
Cannot call value of non-function type 'module<Firebase>'
From the link, this is the exact same code prescribed. I have installed pods for Firebase and Firebase Cloud Messaging.
Cannot call value of non-function type 'module'
The error is saying that you imported a module named Firebase here:
import UIKit
import Firebase
And then you tried calling the module as if it's a function:
override func viewDidLoad() {
super.viewDidLoad()
var myRootRef = Firebase(...)
I think the old Firebase module must have defined a function/class called Firebase, so that when you imported the old Firebase module, that made the Firebase() function/initializer available. Apparently, the new Firebase module doesn't define that function/class anymore, hence the need for the new instructions.
In addition, your url can't be correct:
url:"https://<YOUR-FIREBASE-APP>.firebaseio.com"
You would substitute the actual name of your app in place of <YOUR-FIREBASE-APP>.
In any case, you don't specify a url in your code anymore: instead you call FIRApp.configure() in AppDelegate.swift, and that takes care of connecting to the proper url for you. How does configure() know which url to connect to? Look in the GoogleService-Info.plist file that you dragged into your app--you’ll see the db url in there.
To get a reference to the top of your db, all you do is:
dbRef = FIRDatabase.database().reference()
Here it is in a ViewController:
class ViewController: UIViewController {
var dbRef: FIRDatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
dbRef = FIRDatabase.database().reference()
dbRef.child("person").child("name").setValue("Joe")
After running your app, you'll see something like the following at the Firebase website:
abcd-1234
|____person
|__name: “Joe”
I found a tutorial that works. Its by google so its up to date with the new Firebase.
https://codelabs.developers.google.com/codelabs/firebase-ios-swift/#0