Server Side Events in Swift?

I am new to Swift and would like to subscribe to a server side event. The SDK documentation gives example code for Swift and Obj-C, except for the section on server side events which is only in Obj-C. I have tried to rewrite in Swift but my knowledge of Swift is still very new. Could someone help with changing this code to be Swift compatible

// The event handler:
SparkEventHandler handler = ^(SparkEvent *event, NSError *error) {
        if (!error)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"Got Event %@ with data: %@",event.event,event.data);
            });
        }
        else
        {
            NSLog(@"Error occured: %@",error.localizedDescription);
        }

    };

// This line actually subscribes to the event stream:
id eventListenerID = [[SparkCloud sharedInstance] subscribeToAllEventsWithPrefix:@"temp" handler:handler];

I haven’t had time to actually compile and test this because the IOS SDK example won’t build for me, but this should be close:

EDIT: code deleted because it was wrong.
1 Like

Hi.

Thanks for coming back with this. It all works except the last line

SparkCloud.sharedInstance().subscribeToAllEventsWithPrefix(“temp”, handler)

where I get the following compile error

Cannot convert value of type ‘SparkEventHandler’ (aka ‘(event: SparkEvent, error: ImplicitlyUnwrappedOptional) -> ()’) to expected argument type ‘SparkEventHandler!’

Any ideas?

Got the example to compile and now I see what is wrong. Here’s the corrected, tested code:

// in Spark-SDK-Bridging-Header.h
// #import "Spark-SDK.h"

//  EventViewController.swift
import UIKit


class EventViewController: UIViewController {
    
    func subscribeHandler(event: SparkEvent!, error: NSError!)->() {
        if (error == nil)
        {
            dispatch_async(dispatch_get_main_queue(), {
                NSLog("got event with name:\(event.event) and data:\(event.data)");
            });
        } else {
            NSLog("Error occured:\(error.localizedDescription)")
        }
    }

    @IBAction func subscribe(sender: AnyObject) {
        SparkCloud.sharedInstance().subscribeToAllEventsWithPrefix("test-", handler: subscribeHandler)
    }
}

Thanks so much with this, I have it all working now :grinning:

HI,
This has got me part of the way there, but I am receiving the same error as above on the last line.

Cannot convert value of type ‘SparkEventHandler’ (aka ‘(event: SparkEvent, error: ImplicitlyUnwrappedOptional) -> ()’) to expected argument type ‘SparkEventHandler!’

I am still learning Swift so a little confused by this error, please help

Here’s a working piece of Swift code that subscribes to events: (iOS SDK v0.4.0)

            print("subscribing to event...");
        var gotFirstEvent : Bool = false
        myEventId = myPhoton!.subscribeToEventsWithPrefix("test", handler: { (event: SparkEvent?, error:NSError?) -> Void in
            if (!gotFirstEvent) {
                print("Got first event: "+event!.event)
                gotFirstEvent = true
                dispatch_group_leave(deviceGroup)
            } else {
                print("Got event: "+event!.event)
            }
        });

If possible, could you share a bit more of the swift code? I’m about to try this tomorrow and am also learning swift in the process. Could you include how you set up the spark connection in the first place?

I suggest you check out the documentation on SDK usage:

Thanks so much. I didn’t even think to look there. There’s so much new stuff now I really should go through and check it all out again.