0

I used to build my app on Firebase before and there was a method which listens for value updates, something like this:

refHandle = postRef.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
  let postDict = snapshot.value as! [String : AnyObject]
  // ...
})

Now I'm not using firebase anymore, I'm using deployd and I use Alamofire to retrieve data in JSON. I wonder if there is an event listener in Alamofire that can execute code if the value is changing in the database, instead of retrieving the value every 2 minutes.

Thanks.

Lawris
  • 965
  • 2
  • 9
  • 21

1 Answers1

2

Okay so I found this thing called TRVSEventSource which is meant for handling SSE events.

So I added the following code after adding the header files and bridging them like this:

let configs = NSURLSessionConfiguration.defaultSessionConfiguration()
    configs.HTTPAdditionalHeaders = ["Accept" : "text/event-stream"]

    let eventsource = TRVSEventSource(URL: NSURL(string: "https://app.firebaseio.com/about.json?auth=<Your Database Secret>"), sessionConfiguration: configs)
    eventsource.delegate = self

    eventsource.open()

After that using the TRVSEventSourceDelegate, I added this delegate to get the information:

 func eventSource(eventSource: TRVSEventSource!, didReceiveEvent event: TRVSServerSentEvent!) {
    do{
        let data = try NSJSONSerialization.JSONObjectWithData(event.data, options: .MutableContainers)
        print(data)
    }
    catch let error
    {
        print(error)
    }
}

The following prints something like this { data = { desc = "My Data"; }; path = "/"; }

And with that also tells you within what path of the JSOn file has been edited or added, idk how to handle things separately and stuff but I think you can handle the rest XD. Not a good answer but I hope I helped XD (First time properly answering something)

  • This is a very good answer ! Thanks a lot :-) ; Just, where did you find this ? – Lawris Aug 22 '16 at 19:25
  • My luck, i was just putting random keywords, doing research on handling SSE's and other rubbish and I found this XD, also I have no idea how to handle the rest so I'm just gonna make it call a func that will make a request again using Alamofire, basically like a refresh button cause I have no idea how to read this data XD. – daniyalkhan2000 Aug 22 '16 at 19:29
  • I'm actually using this for my Apple Watch app, sort of like a companion app for my main one and I found just what I needed. – daniyalkhan2000 Aug 22 '16 at 19:36