Issue
I am attempting to write the contents of a custom object to FireBase Firestore. When the method to write to the database is called, neither my OnSuccessListener nor my OnFailureListener seems to be triggering and the data is not writing to the store.
I am getting the following exception in my debug log:
2022-02-04 17:14:45.363 22150-22227/com.skillstorm.helloworld W/Firestore: (24.0.1) [WriteStream]: (915220b) Stream closed with status: Status{code=UNAVAILABLE, description=null, cause=java.net.SocketException: socket failed: EPERM (Operation not permitted)
at java.net.Socket.createImpl(Socket.java:492)
at java.net.Socket.<init>(Socket.java:446)
at java.net.Socket.<init>(Socket.java:250)
at javax.net.DefaultSocketFactory.createSocket(SocketFactory.java:285)
at io.grpc.okhttp.OkHttpClientTransport$4.run(OkHttpClientTransport.java:558)
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:133)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
Here is the code for my custom object:
public class Task implements Serializable {
// Class properties
private String title;
private Date dateCreated;
private boolean completed;
private Date dueDate;
private String description;
// Constructors
public Task (String title, Date dateCreated, boolean completed, Date dueDate) {
this.title = title;
this.dateCreated = dateCreated;
this.completed = completed;
this.dueDate = dueDate;
this.description = generateDescription(title, dueDate, completed);
}
public Task() {}
// Getters / Setters
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
this.description = generateDescription(this.title, this.dueDate, this.completed);
}
public Date getDateCreated() {
return this.dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public boolean getCompleted() {
return this.completed;
}
public void setCompleted(boolean completed) {
this.completed = completed;
this.description = generateDescription(this.title, this.dueDate, this.completed);
}
public Date getDueDate() {
return this.dueDate;
}
public void setDueDate(Date dueDate) {
this.dueDate = dueDate;
this.description = generateDescription(this.title, this.dueDate, this.completed);
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = generateDescription(this.title, this.dueDate, this.completed);
}
// Custom methods
private String generateDescription (String taskTitle, Date taskDueDate, boolean isCompleted) {
String generatedDescription = "'" + taskTitle + "'";
if (taskDueDate != null) {
generatedDescription = generatedDescription + " due on " + taskDueDate.toString();
}
if (isCompleted) {
generatedDescription = generatedDescription + " has been completed!";
}
else {
generatedDescription = generatedDescription + " has not yet been completed.";
}
return generatedDescription;
}}
Here is the method attempting to write to the database:
private void updateTaskList(Task task) {
CollectionReference databaseTasks = database.collection("Tasks");
databaseTasks.add(task).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(MainActivity.this, "Your task has been added to the database!", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Your task could not be added to the database...", Toast.LENGTH_LONG).show();
}
});
}
And here is the onActivityResult() method in my Main Activity that is calling the method to write to the database:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == Activity.RESULT_OK) {
Task newTask = (Task) data.getSerializableExtra("New Task");
taskList.add(newTask);
populateTaskList();
updateTaskList(newTask);
}
}
}
Finally, here is the rule from my Firestore instance:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}}}
Solution
Solved. Here are the steps I had to take to resolve:
Uninstall app from the test device.
Change Firestore rules to:
rules_version= '2'; service cloud.firestore{ match /databases/{database}/documents { match /{document=**} { allow read, write: if true; } } }
(Note: This is not good practice and is only a quick solution for development purposes)
- Run the app again and write to Firestore seems to work now
Answered By - John Mack
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.