Eureka: How to populate the form Values from Firebase database ?

Created on 17 Jan 2017  路  7Comments  路  Source: xmartlabs/Eureka

I am using Eureka to create forms in my project, thanks for the great read me.

I have sucesfully saved the form values into my database. Now i am looking to populate the for values with the saved values from the firebase database. How can i achieve this ?

Stack Overflow awaiting response question

Most helpful comment

I'm not sure if anyone is still interested in this.

I'm half-done with writing an adapter to convert between Eureka-friendly values and FirebaseDatabase-friendly values. Is there any way I can identify the row's type or data type from a BaseRow?

Eureka+Firebase.swift

import Foundation
import Eureka

public extension Form {
    public func valuesForFirebase(includeHidden: Bool = false) -> [String: Any?]{
        if includeHidden {
            return allRows.filter({ $0.tag != nil })
                .reduce([String: Any?]()) {
                    var result = $0
                    result = result.firebaseFriendlyDictionary()
                    result[$1.tag!] = $1.baseValue
                    return result
            }
        }
        return rows.filter({ $0.tag != nil })
            .reduce([String: Any?]()) {
                print(type(of: $0))
                var result = $0
                result = result.firebaseFriendlyDictionary()
                result[$1.tag!] = $1.baseValue
                return result
        }
    }
}

private extension Dictionary {
    func firebaseFriendlyDictionary() -> [String: Any?] {
        return self.reduce([:], { (dictionary, tuple) -> [String: Any?] in
            var dictionary = dictionary
            if tuple.value is NSDate {
                dictionary[tuple.key as! String] = (tuple.value as! NSDate).timeIntervalSince1970
            } else {
                dictionary[tuple.key as! String] = tuple.value
            }
            return dictionary
        })
    }

    func eurekaFriendlyDictionary(forForm form: Form) -> [String: Any?] {
        // return friendly dict here!
    }
}

All 7 comments

You'll have to observe a Fireabase Database reference child, and set your form values in the callback. See the Read and Write Data on iOS Firebase guide.

@sriteja25 have you get around this?

Sorry, as we had to customise the text fields and other UIElements , so to gain control I built my own form from scratch, no longer using Eureka now.

I'm not sure if anyone is still interested in this.

I'm half-done with writing an adapter to convert between Eureka-friendly values and FirebaseDatabase-friendly values. Is there any way I can identify the row's type or data type from a BaseRow?

Eureka+Firebase.swift

import Foundation
import Eureka

public extension Form {
    public func valuesForFirebase(includeHidden: Bool = false) -> [String: Any?]{
        if includeHidden {
            return allRows.filter({ $0.tag != nil })
                .reduce([String: Any?]()) {
                    var result = $0
                    result = result.firebaseFriendlyDictionary()
                    result[$1.tag!] = $1.baseValue
                    return result
            }
        }
        return rows.filter({ $0.tag != nil })
            .reduce([String: Any?]()) {
                print(type(of: $0))
                var result = $0
                result = result.firebaseFriendlyDictionary()
                result[$1.tag!] = $1.baseValue
                return result
        }
    }
}

private extension Dictionary {
    func firebaseFriendlyDictionary() -> [String: Any?] {
        return self.reduce([:], { (dictionary, tuple) -> [String: Any?] in
            var dictionary = dictionary
            if tuple.value is NSDate {
                dictionary[tuple.key as! String] = (tuple.value as! NSDate).timeIntervalSince1970
            } else {
                dictionary[tuple.key as! String] = tuple.value
            }
            return dictionary
        })
    }

    func eurekaFriendlyDictionary(forForm form: Form) -> [String: Any?] {
        // return friendly dict here!
    }
}

@liaujianjie thanks for sharing your code! We'll take in mind for future reference.

I'm closing this issue.

@sriteja25 can you tell me how did you saved value in the database?

Was this page helpful?
0 / 5 - 0 ratings