Skip to content

Instantly share code, notes, and snippets.

@sferoze
Created July 22, 2015 13:49
Show Gist options
  • Save sferoze/80f1a3c2079e1b3b9768 to your computer and use it in GitHub Desktop.
Save sferoze/80f1a3c2079e1b3b9768 to your computer and use it in GitHub Desktop.
Example View Controller showing DDP package integration.
import UIKit
import Foundation
class ViewController: UIViewController, SubscriptionLoaderDelegate {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Meteor.loginWithEmail("[email protected]", password: "password") { (error) -> Void in
if error != nil {
println(error.localizedFailureReason)
} else {
println("logging in worked")
self.callSetsMethod()
}
}
}
func callSetsMethod() {
Meteor.callMethodWithName("getSetsMobile", parameters: nil) { result in
println(result.0)
}
let subscriptionLoader = SubscriptionLoader()
var subscription = subscriptionLoader.addSubscriptionWithName("mySets")
self.subscriptionLoader(subscriptionLoader, subscription: subscription, didFailWithError: NSError())
}
func subscriptionLoader(subscriptionLoader: SubscriptionLoader, subscription: METSubscription, didFailWithError error: NSError) {
subscriptionLoader.delegate = self
subscriptionLoader.whenReady {
var sets = Meteor.database.collectionWithName("sets")
var allSets: NSArray = sets.allDocuments
println(allSets[0].valueForKey("fields")!["name"]!)
let managedContext = Meteor.mainQueueManagedObjectContext!
managedContext.save(nil)
self.getSetsFromLocalStorage()
}
}
func fetch(entityName entity: String, sortedBy sorter: NSSortDescriptor?, withPredicate predicate: NSPredicate?) -> [NSManagedObject]? {
let managedContext = Meteor.mainQueueManagedObjectContext!
let fetchRequest = NSFetchRequest(entityName: entity)
if let sort: NSSortDescriptor = sorter {
fetchRequest.sortDescriptors = [sort]
}
if let pred: NSPredicate = predicate {
fetchRequest.predicate = pred
}
var error: NSError?
let fetchedResults =
managedContext.executeFetchRequest(fetchRequest,
error: &error) as! [NSManagedObject]?
if let results = fetchedResults {
return results
} else {
println("Could not fetch \(error), \(error!.userInfo)")
return nil
}
}
func getSetsFromLocalStorage() {
var tempEntity: [NSManagedObject]? = self.fetch(entityName: "Set", sortedBy: nil, withPredicate: nil)
if let entity: [NSManagedObject] = tempEntity {
//println(entity.valueForKey("name"))
//let entityArray = entity as? [NSManagedObject]
for item in entity {
println(item.valueForKey("name")!)
}
// if let entityArray = entityArray {
// for item in entityArray {
// println(item.valueForKey("name")!)
// }
//
// }
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment