IN THIS ARTICLE

    When developing apps for a certain platform, every programmer runs into a familiar set of basic problems. How do I update every item in a collection efficiently? How do I prevent a mesage from being sent twice to the recipient? How do I find this element in an unwieldy list of objects of different types? When we reduce the time we spend solving these problems, app creation becomes much faster.

    From my experience developing iOS apps, I’ve found that updating objects across multiple apps based on certain criteria is one of these problems. Various reasons exist for this; one common one is that these objects are views, which are complex to support various UI features. Another is that to synchronize the value of a certain view requires a service capable of broadcasting the message at extremely high speeds, especially if the app constantly updates it. Finding an answer to this problem would help developers make apps work together, and significantly reduce the amount code needing to be written.

    Key-Value Observing and PubNub

    So how can you deal with the issues stated above? One solution is to use the PubNub framework and the key-value observing capabilities provided by Cocoa to build a library. You can check out the framework in its entirety on our GitHub repository: https://github.com/pubnub/PubNubKeyAutoUpdate.

    In the Cocoa framework, key-value observing is implemented by the NSKeyvalueObserving protocol. To observe for these changes, the addObserver method is used. By setting an object to observe for changes in key-values, I am able to capture changes in values triggered by client side applications. To broadcast messages on a channel, the sendMessage class method is used. This allows me to broadcast a message on a channel, and have multiple clients or applications listening to it.

    Structuring the application

    Key Value Observing With PubNub

    To properly structure the application, the library should have three sets of files. One set should provide a user of the library with a clear set of class methods, with descriptive names that clearly define what the method does. In my library, I create a file called PubNubKeyUpdate which has a method subscribeToChannelWithUpdates, which creates an instance of another class, and sends a selector to that instance.

    Another set will execute the logic that allows for the keys to be synchronized. In my library, I name the file AutoUpdateImplementation, and use two methods to achieve my goal. The first one is called subscribeToChannelWithUpdates.

    This code can be seen below:

    The method instantiates instance variables, adds observers and listens to messages on a channel using a function call. The callback supplied as an argument to this function is used to process messages received by it. In the callback, I go through the keys stored in the variable, and if the keys are found and the string is a properly formatted dictionary, I set the key to the value specified by sending the setValue selector to sender.

    observeValueForKeyPath is a function specified by the Cocoa framework. It is triggered if any of the sender’s(view controller’s) keys are changed. If this happens, the name of the key is matched against the names of the keys which instance stores. If a match is found, the key and value are broadcast on the server channel, using the PubNub sendMessage class method.

    All of this functionality is abstracted away, and the user uses a class function called subscribeToChannelWithUpdates and lets the library do all the work behind the scenes.

    The methods check for errors using a try-catch block. While there are error handling methods inbuilt and contained in a separate file, the user of the library, when calling subscribeToChannelWithUpdates must supply a block variable as an argument, which is executed every time an exception occurs.

    Putting it to use

    To demonstrate the cross-device and cross-application functionality, I included a demo with two separate applications. Both applications subscribe to the channel in their respective view controllers, using the following line of code:

    One application models a private finance application, and simply allows a user to increase or decrease the amount of money he has (shown as a label). As the user increases or decreases his/her money, the library observes the value being changed for the label’s text key, and immediately broadcasts this message on a channel.

    Another application, modeling a banker’s interface, subscribes to the same channel with the same keys. The value for that person’s balance is automatically updated. The bank application gives it’s own user the option of entering in arte and adding that amount to the balance. Once this is done, the value is automatically updated in the personal finance application.

    Wrap up

    One of the key aims of this library was to reduce the amount of code the user of PubNub libraries has to write. If we look at the bank application’s code, it comes to 70 lines. The private finance application’s code is 51 lines. The app designer does not have to have to worry about sending any messages regarding the keys, or worry about passing data and values from the view controller to the app delegate or other messy situations. This library is just a sample use case; following the design principles it incorporates, developers can build bigger and better libraries which can substantially reduce code needing is specific use cases.

    You can check out the framework in its entirety on our GitHub repository: https://github.com/pubnub/PubNubKeyAutoUpdate.

    Try PubNub today!

    Build realtime applications that perform reliably and securely, at global scale.
    Try Our APIs
    Try PubNub today!
    More From PubNub