Created
August 19, 2014 13:11
-
-
Save xpqz/cf8140069279b2aa58f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Cloudant.swift | |
// | |
// Created by Stefan Kruger on 19/08/2014. | |
// Copyright (c) 2014 Stefan Kruger. All rights reserved. | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | |
// except in compliance with the License. You may obtain a copy of the License at | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// Unless required by applicable law or agreed to in writing, software distributed under the | |
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
// either express or implied. See the License for the specific language governing permissions | |
// and limitations under the License. | |
import Foundation | |
typealias handlerBlock = () -> Void | |
class Cloudant:NSObject, CDTReplicatorDelegate { | |
var username:String | |
var apiPassword:String | |
var apiKey:String | |
var database:String | |
var manager:CDTDatastoreManager | |
var datastore:CDTDatastore | |
var replicatorFactory:CDTReplicatorFactory? | |
var replicator:CDTReplicator? | |
var replHandler: handlerBlock? | |
var indexManager:CDTIndexManager? | |
var urlBase:String { | |
return "https://\(apiKey):\(apiPassword)@\(username).cloudant.com/\(database)" | |
} | |
init(database:String, username:String, key:String, password:String) { | |
self.database = database | |
self.username = username | |
self.apiKey = key | |
self.apiPassword = password | |
// Set up the local data store | |
var outError:NSError? | |
let fileManager = NSFileManager.defaultManager() | |
let documentsDir = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL | |
let storeURL = documentsDir.URLByAppendingPathComponent("cloudant-sync-datastore") | |
let path = storeURL.path | |
manager = CDTDatastoreManager(directory:path, error:&outError) | |
if let error = outError { | |
println("Error creating datastore manager: \(error.localizedDescription)") | |
} | |
datastore = manager.datastoreNamed(database, error:&outError) | |
if let error = outError { | |
println("Error creating datastore: \(error.localizedDescription)") | |
} | |
indexManager = CDTIndexManager(datastore: datastore, error: &outError) | |
if let error = outError { | |
println("Error creating index manager: \(error.localizedDescription)") | |
} | |
super.init() | |
} | |
func startReplicationWithHandler(completionHandler:handlerBlock) { | |
var outError:NSError? | |
// Create and start the replicator factory | |
replicatorFactory = CDTReplicatorFactory(datastoreManager: manager) | |
replicatorFactory!.start() | |
// Make a pull replicator, with ourselves as the delegate | |
let replication = CDTPullReplication(source: NSURL(string:urlBase), target: datastore) | |
replicator = replicatorFactory!.oneWay(replication, error: &outError) | |
if let error = outError { | |
println("Error creating replicator: \(error.localizedDescription)") | |
} | |
replHandler = completionHandler | |
replicator!.delegate = self | |
replicator!.start() | |
} | |
func document(docid:String) -> CDTDocumentRevision? { | |
var outError:NSError? | |
let retrieved = datastore.getDocumentWithId(docid, error: &outError) | |
if let error = outError { | |
println("Error fetching document '\(docid)': \(error.localizedDescription)") | |
return nil | |
} | |
return retrieved | |
} | |
// MARK: CDTReplicatorDelegate | |
func replicatorDidComplete(replicator: CDTReplicator!) { | |
if let handler = replHandler { | |
handler() | |
} | |
} | |
func replicatorDidChangeProgress(replicator: CDTReplicator!) { | |
let state = CDTReplicator.stringForReplicatorState(replicator!.state) | |
println(state) | |
} | |
func replicatorDidChangeState(replicator: CDTReplicator!) { | |
let state = CDTReplicator.stringForReplicatorState(replicator!.state) | |
println(state) | |
} | |
func replicatorDidError(replicator: CDTReplicator!, info: NSError!) { | |
let state = CDTReplicator.stringForReplicatorState(replicator!.state) | |
println(state) | |
println(info.localizedDescription) | |
} | |
} | |
do you have an example of how we might use indexManager to query for certain documents? thx.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use, install the Cloudant libs from https://github.com/cloudant/CDTDatastore and put
in your bridging header. Then you can do something like this: