CRUD 操作の実行
データ・ストアの内容を変更できます。
このタスクについて
作成、取得、更新、および削除 (CRUD) 操作について詳しくは、CDTDatastore CRUD の資料を参照してください。
リモート・ストアでの作成、取得、更新、および削除 (CRUD) 操作については、Cloudant® 文書 API を参照してください。
データの作成
手順
データを保存します。
変更後 (Cloudant Sync を使用):
変更前 (IMFData/CloudantToolkit を使用):
// Use an existing store
CDTStore *store = existingStore;
// Create your Automobile to save
Automobile *automobile = [[Automobile alloc] initWithMake:@"Toyota" model:@"Corolla" year: 2006];
[store save:automobile completionHandler:^(id savedObject, NSError *error) {
if (error) {
// save was not successful, handler received an error
} else {
// use the result
Automobile *savedAutomobile = savedObject;
NSLog(@"saved revision: %@", savedAutomobile);
}
}];
// Use an existing store
let store:CDTStore = existingStore
// Create your object to save
let automobile = Automobile(make: "Toyota", model: "Corolla", year: 2006)
store.save(automobile, completionHandler: { (savedObject:AnyObject!, error:NSError!) -> Void in
if nil != error {
//Save was not successful, handler received an error
} else {
// Use the result
print("Saved revision: \(savedObject)")
}
})

// Use an existing store
Store store = existingStore;
// Create your object to save
Automobile automobile = new Automobile("Toyota", "Corolla", 2006);
// Save automobile to store
Task<Object> saveTask = store.save(automobile);
saveTask.continueWith(new Continuation<Object, Void>() {
@Override
public Void then(Task<Object> task) throws Exception {
if (task.isFaulted()) {
// save was not successful, task.getError() contains the error
} else {
// use the result
Automobile savedAutomobile = (Automobile) task.getResult();
}
return null;
}
});
変更後 (Cloudant Sync を使用):// Use an existing store
CDTDatastore *datastore = existingDatastore;
// Create document body
CDTMutableDocumentRevision * revision = [CDTMutableDocumentRevision revision];
revision.body = @{@"@datatype" : @"Automobile", @"make" :@"Toyota", @"model": @"Corolla", @"year" : @2006};
NSError *error = nil;
CDTDocumentRevision *createdRevision = [datastore createDocumentFromRevision:revision error:&error];
if (error) {
// save was not successful, handler received an error
} else {
// use the result
NSLog(@"Revision: %@", createdRevision);
}
// Use an existing store
let datastore:CDTDatastore = existingDatastore
// Create document body
let revision = CDTMutableDocumentRevision()
revision.setBody(["make":"Toyota","model":"Corolla","year":2006])
var createdRevision:CDTDocumentRevision?
do{
createdRevision = try datastore.createDocumentFromRevision(revision)
NSLog("Revision: \(createdRevision)");
}catch let error as NSError{
// Handle error
}

// Use an existing store
Datastore datastore = existingStore;
// Create document body
Map<String, Object> body = new HashMap<String, Object>();
body.put("@datatype", "Automobile");
body.put("make", "Toyota");
body.put("model", "Corolla");
body.put("year", 2006);
// Create revision and set body
MutableDocumentRevision revision = new MutableDocumentRevision();
revision.body = DocumentBodyFactory.create(body);
// Save revision to store
DocumentRevision savedRevision = datastore.createDocumentFromRevision(revision);
データの読み取り
データをフェッチすることができます。
手順
データを読み取ります。
変更前 (IMFData/CloudantToolkit を使用):
変更後 (Cloudant Sync を使用):
変更前 (IMFData/CloudantToolkit を使用):CDTStore *store = existingStore;
NSString *automobileId = existingAutomobileId;
// Fetch Autombile from Store
[store fetchById:automobileId completionHandler:^(id object, NSError *error) {
if (error) {
// fetch was not successful, handler received an error
} else {
// use the result
Automobile *savedAutomobile = object;
NSLog(@"fetched automobile: %@", savedAutomobile);
}
}];
// Using an existing store and Automobile
let store:CDTStore = existingStore
let automobileId:String = existingAutomobileId
// Fetch Autombile from Store
store.fetchById(automobileId, completionHandler: { (object:AnyObject!, error:NSError!) -> Void in
if nil != error {
// Fetch was not successful, handler received an error
} else {
// Use the result
let savedAutomobile:Automobile = object as! Automobile
print("Fetched automobile: \(savedAutomobile)")
}
})

// Use an existing store and documentId
Store store = existingStore;
String automobileId = existingAutomobileId;
// Fetch the automobile from the store
Task<Object> fetchTask = store.fetchById(automobileId);
fetchTask.continueWith(new Continuation<Object, Void>() {
@Override
public Void then(Task<Object> task) throws Exception {
if (task.isFaulted()) {
// fetch was not successful, task.getError() contains the error
} else {
// use the result
Automobile fetchedAutomobile = (Automobile) task.getResult();
}
return null;
}
});
変更後 (Cloudant Sync を使用):// Use an existing store and documentId
CDTDatastore *datastore = existingDatastore;
NSString *documentId = existingDocumentId;
// Fetch the CDTDocumentRevision from the store
NSError *error = nil;
CDTDocumentRevision *fetchedRevision = [datastore getDocumentWithId:documentId error:&error];
if (error) {
// fetch was not successful, handler received an error
} else {
// use the result
NSLog(@"Revision: %@", fetchedRevision);
}
// Use an existing store and documentId
let datastore:CDTDatastore = existingDatastore
let documentId:String = existingDocumentId
var fetchedRevision:CDTDocumentRevision?
do{
fetchedRevision = try datastore.getDocumentWithId(documentId)
NSLog("Revision: \(fetchedRevision)");
}catch let error as NSError{
// Handle error
}

// Use an existing store and documentId
Datastore datastore = existingStore;
String documentId = existingDocumentId;
// Fetch the revision from the store
DocumentRevision fetchedRevision = datastore.getDocument(documentId);
データの更新
オブジェクトを更新する場合は、既存オブジェクトに対して保存を実行します。項目は存在するため、更新されます。
手順
オブジェクトを更新します。
変更前 (IMFData/CloudantToolkit を使用):
変更後 (Cloudant Sync を使用):
変更前 (IMFData/CloudantToolkit を使用):// Use an existing store and Automobile
CDTStore *store = existingStore;
Automobile *automobile = existingAutomobile;
// Update some of the values in the Automobile
automobile.year = 2015;
// Save Autombile to the store
[store save:automobile completionHandler:^(id savedObject, NSError *error) {
if (error) {
// sasve was not successful, handler received an error
} else {
// use the result
Automobile *savedAutomobile = savedObject;
NSLog(@"saved automobile: %@", savedAutomobile);
}
}];
// Use an existing store and Automobile
let store:CDTStore = existingStore
let automobile:Automobile = existingAutomobile
// Update some of the values in the Automobile
automobile.year = 2015
// Save Autombile to the store
store.save(automobile, completionHandler: { (savedObject:AnyObject!, error:NSError!) -> Void in
if nil != error {
// Update was not successful, handler received an error
} else {
// Use the result
let savedAutomobile:Automobile = savedObject as! Automobile
print("Updated automobile: \(savedAutomobile)")
}
})

// Use an existing store and Automobile
Store store = existingStore;
Automobile automobile = existingAutomobile;
// Update some of the values in the Automobile
automobile.setYear(2015);
// Save automobile to store
Task<Object> saveTask = store.save(automobile);
saveTask.continueWith(new Continuation<Object, Void>() {
@Override
public Void then(Task<Object> task) throws Exception {
if (task.isFaulted()) {
// save was not successful, task.getError() contains the error
} else {
// use the result
Automobile savedAutomobile = (Automobile) task.getResult();
}
return null;
}
});
変更後 (Cloudant Sync を使用):// Use an existing store and document
CDTDatastore *datastore = existingDatastore;
CDTMutableDocumentRevision *documentRevision = [existingDocumentRevision mutableCopy];
// Update some of the values in the revision
[documentRevision.body setValue:@2015 forKey:@"year"];
NSError *error = nil;
CDTDocumentRevision *updatedRevision = [datastore updateDocumentFromRevision:documentRevision error:&error];
if (error) {
// save was not successful, handler received an error
} else {
// use the result
NSLog(@"Revision: %@", updatedRevision);
}
// Use an existing store and document
let datastore:CDTDatastore = existingDatastore
let documentRevision:CDTMutableDocumentRevision = existingDocumentRevision.mutableCopy()
// Update some of the values in the revision
documentRevision.body()["year"] = 2015
var updatedRevision:CDTDocumentRevision?
do{
updatedRevision = try datastore.updateDocumentFromRevision(documentRevision)
NSLog("Revision: \(updatedRevision)");
}catch let error as NSError{
// Handle error
}

// Use an existing store and documentId
// Use an existing store
Datastore datastore = existingStore;
// Make a MutableDocumentRevision from the existing revision
MutableDocumentRevision revision = existingRevision.mutableCopy();
// Update some of the values in the revision
Map<String, Object> body = revision.getBody().asMap();
body.put("year", 2015);
revision.body = DocumentBodyFactory.create(body);
// Save revision to store
DocumentRevision savedRevision = datastore.updateDocumentFromRevision(revision);
データの削除
オブジェクトを削除するには、削除するオブジェクトをストアに渡します。
手順
オブジェクトを削除します。
変更前 (IMFData/CloudantToolkit を使用):
変更後 (Cloudant Sync を使用):
変更前 (IMFData/CloudantToolkit を使用):// Using an existing store and Automobile
CDTStore *store = existingStore;
Automobile *automobile = existingAutomobile;
// Delete the Automobile object from the store
[store delete:automobile completionHandler:^(NSString *deletedObjectId, NSString *deletedRevisionId, NSError *error) {
if (error) {
// delete was not successful, handler received an error
} else {
// use the result
NSLog(@"deleted Automobile doc-%@-rev-%@", deletedObjectId, deletedRevisionId);
}
}];
// Using an existing store and Automobile
let store:CDTStore = existingStore
let automobile:Automobile = existingAutomobile
// Delete the Automobile object
store.delete(automobile, completionHandler: { (deletedObjectId:String!, deletedRevisionId:String!, error:NSError!) -> Void in
if nil != error {
// delete was not successful, handler received an error
} else {
// use the result
print("deleted document doc-\(deletedObjectId)-rev-\(deletedRevisionId)")
}
})

// Use an existing store and automobile
Store store = existingStore;
Automobile automobile = existingAutomobile;
// Delete the automobile from the store
Task<String> deleteTask = store.delete(automobile);
deleteTask.continueWith(new Continuation<String, Void>() {
@Override
public Void then(Task<String> task) throws Exception {
if (task.isFaulted()) {
// delete was not successful, task.getError() contains the error
} else {
// use the result
String deletedAutomobileId = task.getResult();
}
return null;
}
});
変更後 (Cloudant Sync を使用):// Use an existing store and revision
CDTDatastore *datastore = existingDatastore;
CDTDocumentRevision *documentRevision = existingDocumentRevision;
// Delete the CDTDocumentRevision from the store
NSError *error = nil;
CDTDocumentRevision *deletedRevision = [datastore deleteDocumentFromRevision:documentRevision error:&error];
if (error) {
// delete was not successful, handler received an error
} else {
// use the result
NSLog(@"deleted document: %@", deletedRevision);
}
// Use an existing store and revision
let datastore:CDTDatastore = existingDatastore
let documentRevision:CDTDocumentRevision = existingDocumentRevision
var deletedRevision:CDTDocumentRevision?
do{
deletedRevision = try datastore.deleteDocumentFromRevision(documentRevision)
NSLog("Revision: \(deletedRevision)");
}catch let error as NSError{
// Handle error
}

// Use an existing store and revision
Datastore datastore = existingStore;
BasicDocumentRevision documentRevision = (BasicDocumentRevision) existingDocumentRevision;
// Delete revision from store
DocumentRevision deletedRevision = datastore.deleteDocumentFromRevision(documentRevision);