Required API Key:
any key with the
addObject
ACL
About this method
Wait for a task to complete before executing the next line of code, to synchronize index updates.
All write operations in Algolia are asynchronous by design.
It means that when you add or update an object to your index, our servers will
reply to your request with a taskID
as soon as they understood the write operation.
The actual insert and indexing will be done after replying to your code.
You can wait for a task to complete by using the `taskID’ and this method.
Check out our full discussion about asynchronous methods.
Examples
Wait for indexing of a new object:
1
2
3
4
5
6
| $index->addObject(
[
'firstname' => 'Jimmie',
'lastname' => 'Barninger'
]
)->wait();
|
1
2
3
4
5
6
| # Asynchronous call and wait until complete
response = index.add_object({ firstname: 'Jimmie', lastname: 'Barninger' })
response.wait
# Synchronous call, that handles the wait internally
index.add_object!({ firstname: 'Jimmie', lastname: 'Barninger' })
|
1
2
3
4
5
6
7
8
9
10
11
| const object = {
firstname: 'Jimmie',
lastname: 'Barninger'
};
index
.saveObject(object, { autoGenerateObjectIDIfNotExist: true })
.wait()
.then(({ objectID }) => {
console.log(objectID);
});
|
1
2
3
4
| index.save_object({
'firstname': 'Jimmie',
'lastname': 'Barninger'
}, {'autoGenerateObjectIDIfNotExist': True}).wait()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| struct Contact: Codable {
let firstName: String
let lastName: String
}
let object = Contact(firstName: "Jimmie",
lastName: "Barninger")
try index.saveObject(object, autoGeneratingObjectID: true) { result in
if case .success(let waitableWrapper) = result {
waitableWrapper.wait() { result in
if case .success(let response) = result {
print("New object is indexed")
}
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| JSONObject object = new JSONObject()
.put("firstname", "Jimmie")
.put("lastname", "Barninger");
index.addObjectAsync(object, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject content, AlgoliaException error) {
if (error != null) {
// Handle error.
} else {
String taskID = content.optString("taskID", null);
if (taskID == null) {
// Handle error.
} else {
index.waitTaskAsync(taskID, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject content, AlgoliaException error) {
if (error == null) {
// Task is published.
}
}
});
}
}
}
});
|
1
2
3
4
5
6
7
8
9
10
11
| Contact contact = new Contact
{
Firstname = "Jimmie",
Lastname = "Barninger"
};
index.SaveObject(contact, autoGenerateObjectId: true).Wait();
// Asynchronous
var resp = await index.SaveObjectsAsync(contacts, autoGenerateObjectId: true);
resp.Wait();
|
1
2
3
4
5
6
7
| BatchIndexingResponse resp = index.saveObject(
new Contact().setFirstname("Jimmie").setLastname("Barninger")
);
// All objects implementing the AlgoliaWaitableResponse interface
// Can be awaited with the .waitTask() method
resp.waitTask();
|
1
2
3
4
5
6
7
8
9
10
11
12
| type Contact struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
contact := Contact{
Firstname: "Jimmie",
Lastname: "Barninger",
}
res, err := index.SaveObject(contact, opt.AutoGenerateObjectIDIfNotExist(true))
err = res.Wait()
|
1
2
3
4
| for {
t <- client.execute { index into "toto" `object` MyObject("test") }
r <- client.execute { waitFor task t from "toto" }
} yield "indexing is done"
|
1
2
3
4
5
6
7
8
| val json = json {
"Firstname" to "Jimmie"
"Lastname" to "Barninger"
}
index.apply {
saveObject(json).wait()
}
|
If you want to ensure multiple objects have been indexed, you need to check
all taskID
s.
Wait for indexing of a new object and send extra http header
1
2
3
4
5
6
7
8
| $index->addObject(
[
'firstname' => 'Jimmie',
'lastname' => 'Barninger'
]
)->wait([
'X-Forwarded-For' => '94.228.178.246'
]);
|
1
2
3
4
5
6
7
| extra_headers = {
'X-Algolia-User-ID': 'user123'
}
time_before_retry = 100
index.wait_task(res['taskID'], time_before_retry, extra_headers)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| const object = {
firstname: 'Jimmie',
lastname: 'Barninger'
};
const waitRequestOptions = {
headers: {
'X-Forwarded-For': '94.228.178.246'
}
};
index
.saveObject(object, { autoGenerateObjectIDIfNotExist: true })
.wait(waitRequestOptions)
.then(({ objectID }) => {
console.log(objectID);
});
|
1
2
3
4
5
6
| index.save_object({
'firstname': 'Jimmie',
'lastname': 'Barninger'
}, {'autoGenerateObjectIDIfNotExist': True}).wait({
'X-Forwarded-For': '94.228.178.246'
})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| struct Contact: Codable {
let firstName: String
let lastName: String
}
let object = Contact(firstName: "Jimmie",
lastName: "Barninger")
var requestOptions = RequestOptions()
requestOptions.headers = ["X-Algolia-User-ID": "user123"]
try index.saveObject(object, autoGeneratingObjectID: true) { result in
if case .success(let waitableWrapper) = result {
waitableWrapper.wait(requestOptions: requestOptions) { result in
if case .success(let response) = result {
print("New object is indexed")
}
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| JSONObject object = new JSONObject()
.put("firstname", "Jimmie")
.put("lastname", "Barninger");
index.addObjectAsync(object, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject content, AlgoliaException error) {
if (error != null) {
// Handle error.
} else {
String taskID = content.optString("taskID", null);
if (taskID == null) {
// Handle error.
} else {
index.waitTaskAsync(taskID, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject content, AlgoliaException error) {
if (error == null) {
// Task is published.
}
}
});
}
}
}
});
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| List<Contact> contacts = new List<Contact>
{
new Contact
{
ObjectID = "myID1",
Firstname = "Jimmie",
Lastname = "Barninger"
},
new Contact
{
ObjectID = "myID2",
Firstname = "Warren",
Lastname = "Speach"
}
};
RequestOptions requestOptions = new RequestOptions
{
Headers = new Dictionary<string,string>{ { "X-Algolia-User-ID", "user123" } }
};
index.SaveObjects(contacts, requestOptions).Wait();
// Asynchronous
var resp = await index.SaveObjectsAsync(contacts, requestOptions);
resp.Wait();
|
1
2
3
4
5
6
7
8
| BatchIndexingResponse resp = index.saveObject(
new Contact().setFirstname("Jimmie").setLastname("Barninger"),
new RequestOptions().addExtraHeader("X-Algolia-User-ID", "user123")
);
// All objects implementing the AlgoliaWaitableResponse interface
// Can be awaited with the .waitTask() method
resp.waitTask();
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| type Contact struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
contact := Contact{
Firstname: "Jimmie",
Lastname: "Barninger",
}
opts := []interface{}{
opt.AutoGenerateObjectIDIfNotExist(true),
opt.ExtraHeaders(map[string]string{"X-Algolia-User-ID": "userID2"}),
}
res, err := index.SaveObject(contact, opts...)
err = res.Wait()
|
1
2
3
4
5
6
7
8
| for {
t <- client.execute { index into "toto" `object` MyObject("test") }
r <- client.execute {
waitFor task t from "toto" options RequestOptions(
extraHeaders = Some(Map("X-Algolia-User-ID" => "user123"))
)
}
} yield "indexing is done"
|
Parameters
taskID
|
taskID of the indexing task to wait for.
|
requestOptions
|
type: list
default: No requestOptions
Optional
A list of request options to send along with the query.
|