Details
-
Task
-
Resolution: Unresolved
-
Major
-
None
-
None
-
None
Description
The Couchbase lite Swift Sample code is before Couchbase had scopes and collections, thus containing depreciated methods. It also doesn't contain a mechanism to keep the program operating, and will instead exit out instantly.
This is a more up to date version
import CouchbaseLiteSwift
|
|
@main
|
struct MyApp {
|
static func main() {
|
|
// Get the database (and create it if it doesn’t exist).
|
let database: Database
|
do {
|
database = try Database(name: "mydb")
|
} catch {
|
fatalError("Error opening database")
|
}
|
|
Database.log.console.domains = .all
|
Database.log.console.level = .verbose
|
|
// Create a new document (i.e. a record) in the database.
|
let mutableDoc = MutableDocument()
|
.setFloat(2.0, forKey: "version")
|
.setString("SDK", forKey: "type")
|
|
let collection: Collection
|
do {
|
collection = try database.createCollection(name:"_default",scope: "_default");
|
} catch {
|
fatalError("Error create collection")
|
}
|
|
|
// Save it to the database.
|
do {
|
try collection.save(document:mutableDoc)
|
} catch {
|
fatalError("Error saving document")
|
}
|
|
|
print("Created document id type \(mutableDoc.id)? with type = \(mutableDoc.string(forKey: "type")!)")
|
|
let existing_doc:Document?
|
do{
|
existing_doc = try collection.document(id: mutableDoc.id)
|
} catch{
|
fatalError("failed to get document")
|
}
|
// Update a document.
|
if let mutableDoc = existing_doc?.toMutable() {
|
mutableDoc.setString("Swift", forKey: "language")
|
do {
|
try collection.save(document:mutableDoc)
|
|
let document = try collection.document(id: mutableDoc.id)!
|
// Log the document ID (generated by the database)
|
// and properties
|
print("Updated document id \(document.id), adding language \(document.string(forKey: "language")!)")
|
} catch {
|
fatalError("Error updating document")
|
}
|
}
|
|
|
// Create a query to fetch documents of type SDK.
|
print("Querying Documents of type=SDK")
|
let query = QueryBuilder
|
.select(SelectResult.all())
|
.from(DataSource.collection(collection))
|
.where(Expression.property("type").equalTo(Expression.string("SDK")))
|
|
// Run the query.
|
do {
|
let result = try query.execute()
|
print("Number of rows :: \(result.allResults().count)")
|
} catch {
|
fatalError("Error running the query")
|
}
|
|
// Create replicators to push and pull changes to and from the cloud.
|
let targetEndpoint = URLEndpoint(url: URL(string: "ws://localhost:4984/traveldb")!)
|
var replConfig = ReplicatorConfiguration(target: targetEndpoint)
|
replConfig.addCollection(collection)
|
replConfig.replicatorType = .pushAndPull
|
|
|
|
// Add authentication.
|
replConfig.authenticator = BasicAuthenticator(username: "sgwuser1", password: "passwordstring")
|
|
// Create replicator (make sure to add an instance or static variable named replicator)
|
let replicator = Replicator(config: replConfig)
|
|
// Listen to replicator change events.
|
replicator.addChangeListener { (change) in
|
print("Error code")
|
if let error = change.status.error as NSError? {
|
print("Error code :: \(error.code)")
|
}
|
}
|
|
|
// Start replication.
|
replicator.start()
|
var limit = 0
|
while (true){
|
|
if(replicator.status.activity != Replicator.ActivityLevel.stopped)
|
{
|
limit = limit + 1
|
|
}
|
|
if (replicator.status.activity == Replicator.ActivityLevel.stopped && limit > 0 ){
|
break;
|
}
|
}
|
|
replicator.stop();
|
|
}
|
|
}
|
|