Wait for operations

Required API Key: any key with the addObject ACL
Method signature
$index->saveObject(...)->wait();
$index->saveObjects(...)->wait();
$index->partialUpdateObject(...)->wait();
$index->partialUpdateObjects(...)->wait();
$index->replaceAllObjects(...)->wait();
$index->deleteObject(...)->wait();
$index->deleteObjects(...)->wait();
$index->deleteBy(...)->wait();
$index->clearObjects(...)->wait();
$index->batch(...)->wait();

$index->setSettings(...)->wait();

$index->saveSynonym(...)->wait();
$index->saveSynonyms(...)->wait();
$index->replaceAllSynonyms(...)->wait();
$index->deleteSynonym(...)->wait();
$index->clearSynonyms(...)->wait();

$index->saveRule(...)->wait();
$index->saveRules(...)->wait();
$index->replaceAllRules(...)->wait();
$index->deleteRule(...)->wait();
$index->clearRules(...)->wait();


$client->moveIndex(...)->wait();
$client->copyIndex(...)->wait();
$client->copyRules(...)->wait();
$client->copySynonyms(...)->wait();
$client->copySettings(...)->wait();

$client->addApiKey(...)->wait();
$client->updateApiKey(...)->wait();
$client->deleteApiKey(...)->wait();
$client->multipleBatch(...)->wait();

Method signature
index.wait_task(taskID)
Method signature
index.waitTask(taskID, callback)
Method signature
index.wait_task(taskID)
Method signature
index.waitTask(withID: taskID, completionHandler: completionHandler)
index.waitTask(
  withID: taskID,
  requestOptions: requestOptions,
  completionHandler: completionHandler
)

Method signature
index.waitTask(taskID, completionHandler)
Method signature
index.WaitTask(taskID)
index.WaitTask(taskID, requestOptions)
Method signature
index.waitTask(taskID)
index.waitTask(taskID, requestOptions)
Method signature
index.WaitTask(taskID)

index.WaitTaskWithRequestOptions(taskID, requestOptions)
Method signature
waitFor task taskID from "indexName"
waitFor task taskID from "indexName" options requestOptions

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:

<?php
$index->addObject(
  [
    'firstname' => 'Jimmie',
    'lastname'  => 'Barninger'
  ]
)->wait();
# Asynchronous call and wait_task
res = index.add_object({ firstname: 'Jimmie', lastname: 'Barninger' })
index.wait_task(res['taskID'])

# Synchronous call, that does the wait_task internally
index.add_object!({ firstname: 'Jimmie', lastname: 'Barninger' })
var object = {
  firstname: 'Jimmie',
  lastname: 'Barninger'
};

index.addObject(object, function(err, content) {
  index.waitTask(content.taskID, function(err) {
    if (!err) {
      console.log('object ' + content.objectID + ' indexed');
    }
  });
});
res = index.add_object({"firstname": "Jimmie",
                       "lastname": "Barninger"})
index.wait_task(res["taskID"])
index.addObject(
  newObject,
  completionHandler: { (content, error) -> Void in
    if error != nil {
        return
    }
    guard let taskID = content!["taskID"] as? Int else {
        return // could not retrieve task ID
    }
    index.waitTask(
      withID: taskID,
      completionHandler: { (content, error) -> Void in
        if error == nil {
            print("New object is indexed!")
        }
      }
    )
  }
)
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.waitTask(taskID, new CompletionHandler() {
          @Override
          public void requestCompleted(JSONObject content, AlgoliaException error) {
            if (error == null) {
              // Task is published.
            }
          }
        });
      }
    }
  }
});
var res = index.AddObject(
  JObject.Parse(
    @"{""firstname"":""Jimmie"",
      ""lastname"":""Barninger""}"
));

// Asynchronous
// var res = await index.AddObjectAsync(JObject.Parse(
//   @"{""firstname"":""Jimmie"", ""lastname"":""Barninger""}"
// ));

index.WaitTask(res["taskID"].ToString());

// Asynchronous
// await index.WaitTaskAsync(res["taskID"].ToString());
//Sync version
//Every Task object, has a method waitForCompletion()

TaskIndexing task = index.addObject(
  new Contact().setFirstname("Jimmie").setLastname("Barninger")
);

task.waitForCompletion();
// or client.waitTask(task.getTaskID());



//Async version
CompletableFuture<AsyncTaskIndexing> task = index.addObject(
  new Contact().setFirstname("Jimmie").setLastname("Barninger")
);

client.waitTask(task.get());
// or client.waitTask(task.get().getTaskID());
object := algoliasearch.Object{
  "firstname": "Jimmie",
  "lastname":  "Barninger",
}

res, err := index.AddObject(object)
err = index.WaitTask(res.TaskID)
for {
  t <- client.execute { index into "toto" `object` MyObject("test") }
  r <- client.execute { waitFor task t from "toto" }
} yield "indexing is done"

If you want to ensure multiple objects have been indexed, you only need to check the highest taskID (last operation)

Wait for indexing of a new object and send extra http header

<?php
$index->addObject(
  [
    'firstname' => 'Jimmie',
    'lastname'  => 'Barninger'
  ]
)->wait([
  'X-FORWARDED-FOR' => '94.228.178.246'
]);
extra_headers = {
  'X-Algolia-User-ID': 'user123'
}

time_before_retry = 100

index.wait_task(res['taskID'], time_before_retry, extra_headers)
client.setExtraHeader('X-FORWARDED-FOR', '94.228.178.246');

var object = {
  firstname: 'Jimmie',
  lastname: 'Barninger'
};

index.addObject(object, function(err, content) {
  index.waitTask(content.taskID, function(err) {
    if (!err) {
      console.log('object ' + content.objectID + ' indexed');
    }
  });
});
request_options = RequestOptions({'algoliaUserID': 'user123'})
index.wait_task(res["taskID"], request_options=request_options)
let requestOptions = RequestOptions()
requestOptions.headers["X-Algolia-User-ID"] = "user123"

index.addObject(
  newObject,
  requestOptions: requestOptions,
  completionHandler: { (content, error) -> Void in
    if error != nil {
        return
    }
    guard let taskID = content!["taskID"] as? Int else {
        return // could not retrieve task ID
    }

    index.waitTask(
      withID: taskID,
      completionHandler: { (content, error) -> Void in
        if error == nil {
            print("New object is indexed!")
        }
      }
    )
  }
)
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.waitTask(taskID, new CompletionHandler() {
          @Override
          public void requestCompleted(JSONObject content, AlgoliaException error) {
            if (error == null) {
              // Task is published.
            }
          }
        });
      }
    }
  }
});
RequestOptions requestOptions = new RequestOptions();
requestOptions.AddExtraHeader("X-Algolia-User-ID", "user123");

var res = index.AddObject(
  JObject.Parse(
    @"{""firstname"":""Jimmie"", ""lastname"":""Barninger""}"
  ),
  requestOptions
);

// Asynchronous
// var res = await index.AddObjectAsync(
//   JObject.Parse(
//     @"{""firstname"":""Jimmie"", ""lastname"":""Barninger""}"
//   ),
//   requestOptions
// );

index.WaitTask(res["taskID"].ToString());

// Asynchronous
// await index.WaitTaskAsync(res["taskID"].ToString());
//Sync version
//Every Task object, has a method waitForCompletion()

TaskIndexing task = index.addObject(
  new Contact().setFirstname("Jimmie").setLastname("Barninger")
);

task.waitForCompletion(
  new RequestOptions().addExtraHeader("X-Algolia-User-ID", "user123")
);
// or
// client.waitTask(
//   task.getTaskID(),
//   new RequestOptions().addExtraHeader("X-Algolia-User-ID", "user123")
// ));



//Async version
CompletableFuture<AsyncTaskIndexing> task = index.addObject(
  new Contact().setFirstname("Jimmie").setLastname("Barninger")
);

client.waitTask(task.get());
// or
// client.waitTask(
//   task.get().getTaskID(),
//   new RequestOptions().addExtraHeader("X-Algolia-User-ID", "user123")
// );
object := algoliasearch.Object{
  "firstname": "Jimmie",
  "lastname":  "Barninger",
}

opts := &algoliasearch.RequestOptions{
  ExtraHeaders: map[string]string{
    "X-Algolia-User-ID": "userID2",
  },
}

res, err := index.AddObjectWithRequestOptions(object, opts)

err = index.WaitTaskWithRequestOptions(res.TaskID, opts)
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
type: string
Required

taskID of the indexing task to wait for.

requestOptions
type: list
Optional
default: No requestOptions

A list of request options to send along with the query.

Response

No response
Php small white
PHP
Ruby small white
Ruby
Javascript small white
JavaScript
Python small white
Python
Swift small white
iOS
Android small white
Android
Csharp small white
C#
Java small white
Java
Go small white
Go
Scala small white
Scala
Php small white
PHP
Ruby small white
Ruby
Javascript small white
JavaScript
Python small white
Python
Swift small white
iOS
Android small white
Android
Csharp small white
C#
Java small white
Java
Go small white
Go
Scala small white
Scala