Skip to content

Commit

Permalink
Merge pull request #5 from AnonymousAliensX/1-concurrent-modification…
Browse files Browse the repository at this point in the history
…-in-the-database-raising-exception

1 concurrent modification in the database raising exception
  • Loading branch information
InvincibleSihag committed Feb 21, 2023
2 parents f828109 + f5a7971 commit ab81e89
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 46 deletions.
2 changes: 1 addition & 1 deletion CachingDatabase/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ publishing {
release(MavenPublication) {
groupId = 'com.github.anonymousaliensx'
artifactId = 'cachingdatabase'
version = '1.03'
version = '1.04'

afterEvaluate {
from components.release
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.anonymousaliens.cachingdatabase;

import androidx.annotation.NonNull;


import com.anonymousaliens.cachingdatabase.Exceptions.MappingException;
import com.anonymousaliens.cachingdatabase.Utilities.ValueMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.firebase.crashlytics.buildtools.reloc.com.google.common.collect.Iterators;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -60,7 +57,13 @@ public long getChildrenCount(){
JsonNode currentNode = this.path.takeToPathGet(this.jsonNode);
if(currentNode == null)
return 0;
return Iterators.size(currentNode.fieldNames());
int count = 0;
Iterator<String> s = currentNode.fieldNames();
while (s.hasNext()){
s.next();
count += 1;
}
return count;
}

public ArrayList<DataSnapShot> getSnapshotArray(){
Expand Down
51 changes: 26 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ Usage :

Add it in your root build.gradle at the end of repositories:
``` groovy
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
Add the dependency in app gradle :
``` groovy
dependencies {
implementation 'com.github.AnonymousAliensX:CachingDatabase:1.02'
}
dependencies {
implementation 'com.github.AnonymousAliensX:CachingDatabase:1.04'
}
```

Enjoy :)
Expand All @@ -25,17 +26,17 @@ Usage :
For listening value changes :-
``` java
CachingDatabase.getInstance().getReference().child("Student").child("Name")
.listen(new ValueListener() {
@Override
public void onSuccess(DataSnapShot dataSnapShot) {
String name = dataSnapShot.getValue(String.class, "default_value");
}
.listen(new ValueListener() {
@Override
public void onSuccess(DataSnapShot dataSnapShot) {
String name = dataSnapShot.getValue(String.class, "default_value");
}

@Override
public void onFailure(Exception e) {
e.printStackTrace();
}
});
@Override
public void onFailure(Exception e) {
e.printStackTrace();
}
});
```
For putting or updating value at specific location :-
``` java
Expand All @@ -47,10 +48,10 @@ If you want to get callback of putValue success, you can use :-

``` java
CachingDatabase.getInstance().getReference().child("Student").child("Name")
.putValue("AnonymousAlien", new CachingReferenceCallbacks() {
@Override
public void onSuccess(String message) {
super.onSuccess(message);
}
});
.putValue("AnonymousAlien", new CachingReferenceCallbacks() {
@Override
public void onSuccess(String message) {
super.onSuccess(message);
}
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.os.Bundle;

import com.anonymousaliens.cachingdatabase.CachingDatabase;
import com.anonymousaliens.cachingdatabase.CallbackClasses.CachingReferenceCallbacks;
import com.anonymousaliens.cachingdatabase.CallbackClasses.ValueListener;
import com.anonymousaliens.cachingdatabase.DataSnapShot;

Expand All @@ -22,6 +23,26 @@ protected void onCreate(Bundle savedInstanceState) {
map.put("name", "sawan");
map.put("class", "12th");
map.put("roll", "121");
CachingDatabase.getInstance().getReference().child("Map").putValue(map, new CachingReferenceCallbacks() {
@Override
public void onSuccess(String message) {
super.onSuccess(message);
CachingDatabase.getInstance().getReference().child("Map").listen(new ValueListener() {
@Override
public void onSuccess(DataSnapShot dataSnapShot) {
System.out.println("Printing child count");
System.out.println(dataSnapShot.getChildrenCount());
}

@Override
public void onFailure(Exception e) {

}
});
}
});


CachingDatabase.getInstance().getReference().child("Student").listen(new ValueListener() {
@Override
public void onSuccess(DataSnapShot dataSnapShot) {
Expand All @@ -30,27 +51,12 @@ public void onSuccess(DataSnapShot dataSnapShot) {
CachingDatabase.getInstance().getReference().child("Student").child("roll").putValue("122");
}
}

@Override
public void onFailure(Exception e) {

}
});
new Thread(new Runnable() {
@Override
public void run() {
while (true){
CachingDatabase.getInstance().getReference()
.child("Student").putValue(new Random().toString());
try {
Thread.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}).start();
// CachingDatabase.getInstance().getReference().child("Student").putValue(map);


}
}

0 comments on commit ab81e89

Please sign in to comment.