/titanium-parse-live-query

Use the Parse & Parse Live Query iOS SDK's in Axway Titanium.

Primary LanguageObjective-COtherNOASSERTION

Parse Live Query in Titanium

Use the Parse & Parse Live Query iOS SDK's in Axway Titanium! Read more about the Parse Live Query API in the official native repository.

Requirements

  • Swift 4.1 or later (embedded into the hook in hooks/)
  • Titanium SDK 6.3.0 or later (7.1.0.GA used in testing)

API'S

Root-Module

Methods

initialize(args: Dictionary)
  • applicationId (String)
  • clientKey (String)
  • server (String)
  • localDatastoreEnabled (Boolean)
createClient(args: Dictionary)
  • applicationId (String)
  • clientKey (String)
  • server (String)
saveObject(args)
  • className (String)
  • parameters (Dictionary)
  • callback (Function)

Constants

EVENT_TYPE_ENTERED
EVENT_TYPE_LEFT
EVENT_TYPE_CREATED
EVENT_TYPE_UPDATED
EVENT_TYPE_DELETED

Client

Methods

reconnect()
disconnect()
isConnected() -> Boolean
subscribeToQuery(query)
  • query (Query)
unsubscribeFromQuery(args)
  • className (String)
  • query (String)

Events

subscribe
  • query (Query)
unsubscribe
  • query (Query)
event
  • type (EVENT_TYPE_*)
  • object (Object)
  • query (Query)
error
  • error (String)
  • query (Query)

Query

Initializer (createQuery(args))

  • className (String)
  • predicate (String, optional, e.g. name = "hans")

Methods

findObjectsInBackground(callback)
  • callback (Function)

Object

Properties

parseClassName (String)
objectId (String)
createdAt (String)
updatedAt (String)
allKeys (Array)

Methods

objectForKey(key) -> Any
setObjectForKey(object, key)
removeObjectForKey(key)
deleteObject()

Compile Swift Libraries

This project uses the following 5 Swift dependencies:

  • Bolds
  • BoldsSwift
  • Parse
  • ParseLiveQuery
  • Starscream

While Bolds and Parse are Obj-C based, the others are dynamic Swift libraries. This projects resolves all dependencies already for you, including setting the Swift version using the hook placed in hooks/.

Right now, Titanium only supports CocoaPods for Hyperloop, so in order to use it for classic modules, you need to create universal "fat" frameworks and strip the unused architectures again (this is taken care of by the SDK already). A universal library can be created by grabbing the frameworks from Debug-iphonesimulator (Simulator architectures) and Debug-iphoneos (Device architectures) and combine them using the following commands:

  1. Install CocoaPods (sudo gem install cocoapods) and run pod install in the native/ directory of this repository
  2. Create the following folder structures: sim/, device/ & universal/
  3. Copy the .framework files from Debug-iphonesimulator to sim/
  4. Copy the .framework files from Debug-iphoneos to device/
  5. Copy the .framework files from device to universal/ (they are the base for universal frameworks)
  6. For BoldsSwift and ParseLiveQuery, copy the Modules/*.swiftmodule to the universal directory of the framework
  7. Use the following command to merge the sim- and device-frameworks together:
lipo -export -output universal/<name>.framework/<name> sim/<name>.framework/<name> device/<name>.framework/<name>
  1. Replace the final frameworks in <module-project>/platform
  2. Make a pull request to this repo, so others can benefit from it as well

These steps are based on a Shell Script used natively.

Note: In the future, this will all be done by CocoaPods. Make sure to follow TIMOB-25927 regarding Swift support in the SDK.

Example

var ParseLiveQuery = require('ti.livequery');

var win = Ti.UI.createWindow({
  backgroundColor: '#fff'
});

var btn1 = Ti.UI.createButton({
  title: 'Initialize Parse',
  top: 100
});

var btn2 = Ti.UI.createButton({
  title: 'Subscribe',
  top: 200
});

btn1.addEventListener('click', function() {
  ParseLiveQuery.initialize({
    applicationId: '',
    clientKey: '',
    server: ''
  });
});

btn2.addEventListener('click', function() {
  var client = ParseLiveQuery.createClient({
    applicationId: '',
    clientKey: '',
    server: ''
  });

  client.addEventListener('subscribe', function(e) {
    Ti.API.info('Subscribed!');
    // Subscribed
  });

  client.addEventListener('unsubscribe', function(e) {
    Ti.API.info('Unsubscribed!');
    // Unsubscribed
  });

  client.addEventListener('event', function(e) {
    Ti.API.info('Event!');

    printObject(e.object);

    // Event received, check with e.type
  });

  client.addEventListener('error', function(e) {
    Ti.API.info('Error!');
    Ti.API.info(e.error);
    // Error received, check with e.error
  });

  var query = ParseLiveQuery.createQuery({
    className: 'Posts',
    predicate: 'name = "Hans"'
  });

  client.subscribeToQuery(query);

  // Get existing objects
  query.findObjectsInBackground(function(e) {
    Ti.API.info(e);
    var objects = e.objects;

    for (var i = 0; i < objects.length; i++) {
      printObject(objects[i]);
    }
  })
});

// Utility method to print objects by using "objectForKey"
function printObject(object) {
  var allKeys = object.allKeys;

  for (var i = 0; i < allKeys.length; i++) {
    var key = allKeys[i];
    var value = object.objectForKey(key);
    Ti.API.info(key + ' = ' + value);
  }
}

win.add(btn1);
win.add(btn2);

win.open();

Author

Hans Knöchel (@hansemannnn / Web)

License

Apache 2.0

Contributing

Code contributions are greatly appreciated, please submit a new Pull-Request!