Firebase Provider Provider URL (cannot convert value to NSURL?)

I am new to Firebase, and I want to save the provider URL, but it gives the error "Can only store objects like NSNumber, NSString, NSDictionary and NSArray." I tried using a different type of method, but it doesn't seem to work, for example, let profilePicUrl = profile.photoURL as Stringorlet profilePicUrl = NSString(NSURL: profile.photoURL)

This is my method

func createFirebaseUser(){

        let key = ref.child("user").childByAutoId().key
        if let user = FIRAuth.auth()?.currentUser{
            for profile in user.providerData{
                let uid = profile.uid
                let providerID = profile.providerID
                let displayName = profile.displayName
                let email = ""
                let profilePicUrl = profile.photoURL


                //let userDict :[String : AnyObject] = ["name": username!"profilePicUrl": profilePicUrl!]
            let profile = Profile(uid: uid, displayName: displayName!,email: email, imageUrl: profilePicUrl)

                let childUpdates = ["/user/\(key)": profile]


                ref.updateChildValues(childUpdates, withCompletionBlock: { (error, ref) -> Void in
                    // now users exist in the database
                    print("user stored in firebase")
                })
            }
        }

and this is a data model

import Foundation

class Profile {

private var _uid: String
private var _displayName: String
private var _email: String?
private var _gender: String!
private var _city: String!
private var _imageUrl: String!

var uid: String {
    return _uid
}

var displayName: String {
    get {
        return _displayName
    }

    set {
        _displayName = newValue
    }
}

var email: String {
    get {
        return _email!
    }

    set {
        _email = newValue
    }
}

var gender: String {
    get {
        return _gender
    }

    set {
        _gender = newValue
    }
}

var city: String {
    get {
        return _city
    }

    set {
        _city = newValue
    }
}

var imageUrl: String {
    get {
        return _imageUrl
    }

    set {
        _imageUrl = newValue
    }
}

init(uid: String, displayName: String, email: String, imageUrl: String) {
    _uid = uid
    _displayName = displayName
    _email = email
    _imageUrl = imageUrl

}
+4
source share
2 answers

You can store only 4 types of NSObjects mentioned in Firebase. But for the most part, data is just a row, and storing rows is easy.

Assuming your photoURL is the actual NSURL, you can save it as a string

let ref = myRootRef.childByAppendingPath("photo_urls")
let thisFileRef = ref.childByAutoId()
thisFileRef.setValue(photoURL.absoluteString)

NSURL

let url = NSURL(string: urlString)!

, , Firebase. ,

let usersRef = self.myRootRef.childByAppendingPath("users")
let thisUserRef = usersRef.childByAutoId()

var dict = [String: String]()
dict["displayName"] = "Bill"
dict["email"] = "bill@thing.com"
dict["gender"] = "male"
dict["photo_url"] = photoURL.absoluteString

thisUserRef.setValue(dict)

User,

let aUser = User()
aUser.initWithStuff(displayName, email, gender etc etc
aUser.createUser()

.

, auth.uid node. v3.x, 2.x, , , Firebase .

+4

Firebase URL (, Firebase node - ). . , , Firebase, , , URL.

, , URL- :

http://storage.example.com/photos/photo1.jpg

URL :

prefix = http://storage.example.com/
relativeUrl = photos/photo1.jpg

, , node Firebase relativeUrl .

, URL-, .

+1

All Articles