I am calling a method CreateOne
from Non-Activity
class and CreateOne
method needs to return an Object
after Task
execution. So I have addded the below line after calling the Task.
task.wait();
But it is returning error like
object not locked by thread before wait()
Here is my full code:
public class MyStitchHelper
{
public Object CreateOne(Map<String, Object> map) throws InterruptedException
{
Object returnObject = null;
Document mapDoc = new Document(map);
ArrayList<Document> list = new ArrayList<>();
list.add(mapDoc);
Document itemsDocument = new Document("items", list);
PipelineStage itemsStage = new PipelineStage("literal", "", itemsDocument);
Document AuthDocument = new Document();
AuthDocument.append("database","my_db");
AuthDocument.append("collection", _collectionName);
PipelineStage AuthStage = new PipelineStage("insert", "mongodb-atlas", AuthDocument);
Task<List<Object>> task = _client.executePipeline(itemsStage, AuthStage);
task.wait();
if(task.isSuccessful())
{
Log.e("UserDAL", task.getResult().toString());
returnObject = task.getResult();
}
else
{
Log.e("UserDAL", "Error Adding Collectionsss");
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("UserDAL", "" + e.getMessage());
}
});
}
return returnObject;
}
}
Calling Method:
public void AddUser()
{
MyStitchHelper DBHelper = new MyStitchHelper();
Map<String, Object> map = new HashMap<>();
map.put(User.Columns.EMAIL,user.get_userName());
map.put(User.Columns.PASSWORD, user.get_password());
map.put(User.BaseColumns.CREATED_DATE, Calendar.getInstance().getTime());
Document doc = (Document) DBHelper.CreateOne(map);
}
First of all, whether the implementation is correct of what I am trying to do is just creating a new user and returning the Document Object after creation.
And why the error throws?
Any Ideas?
Here´s Question about a similar Issue. It might be usefull
in short from top answer:
Quote: "Here's how wait and notify were meant to be used:"
private Queue<Product> q = ...;
private Object lock = new Object();
void produceSomething(...) {
Product p = reallyProduceSomething();
synchronized(lock) {
q.add(p);
lock.notify();
}
}
void consumeSomething(...) {
Product p = null;
synchronized(lock) {
while (q.peek() == null) {
lock.wait();
}
p = q.remove();
}
reallyConsume(p);
}