【发布时间】:2019-11-26 21:52:50
【问题描述】:
我有两个相同类型的模型对象,用户和机构。 用户对象从 Firebase.Auth 用户获取其 uid。 该机构通过首先创建一个 doucmentReference 然后使用文档引用 uid 设置对象来获取其 uid。
我得到了两者的参考 uid
用户对象触发 addOnSuccessListener 就好了。 机构对象不会触发 addOnSuccessListener 或 addOnFailureListener
这两个对象都只使用 Firebase 支持的数据类型。
这里是插入方法和对象类:
public Observable<QueryResult> insert(Fetchable fetchable) {
Query query = new Query.QueryBuilder().insert(fetchable).build();
return Observable.create(emitter -> {
CollectionReference collectionReference = getCollectionReference(fetchable);
DocumentReference documentReference;
if(fetchable.getType() == Fetchable.Type.USER){
//USER type uses User UID from FireBase Authentication
documentReference = collectionReference.document(fetchable.getRemoteId());
}else{
documentReference = collectionReference.document();
String id = documentReference.getId();
fetchable.setRemoteId(id);
}
Timber.d(documentReference.getPath());
documentReference.set(fetchable).addOnSuccessListener(aVoid -> {
QueryResult queryResult = new QueryResult(query, fetchable);
Timber.d(queryResult.toString());
emitter.onNext(queryResult);
emitter.onComplete();
}).addOnFailureListener(e ->{
QueryResult queryResult = new QueryResult(query, e);
Timber.d(queryResult.toString());
emitter.onNext(queryResult);
emitter.onComplete();
});
});
public final class Institution implements Fetchable {
public static final String KEY = "institution";
@PrimaryKey(autoGenerate = true)
private int localId;
private String remoteId;
private String name;
private String postalCode;
private List<String> instructionProviderIds;
private List<String> adminIds;
private String creationDate;
private String lastUpdate;
private String createdBy;
private String lastUpdatedBy;
private List<String> accessList;
@Ignore
private final Fetchable.Type type = Type.INSTITUTION;
@Ignore
private Institution() {
instructionProviderIds = new ArrayList<>();
adminIds = new ArrayList<>();
accessList = new ArrayList<>();
}
public Institution(int localId, String remoteId, String name, String postalCode, List<String> instructionProviderIds, List<String> adminIds, String creationDate, String lastUpdate, String createdBy, String lastUpdatedBy, List<String> accessList) {
this.localId = localId;
this.remoteId = remoteId;
this.name = name;
this.postalCode = postalCode;
this.instructionProviderIds = instructionProviderIds;
this.adminIds = adminIds;
this.creationDate = creationDate;
this.lastUpdate = lastUpdate;
this.createdBy = createdBy;
this.lastUpdatedBy = lastUpdatedBy;
this.accessList = accessList;
}
public int getLocalId() {
return localId;
}
public String getRemoteId() {
return remoteId;
}
public void setRemoteId(String remoteId) {
this.remoteId = remoteId;
}
public String getLastUpdate() {
return lastUpdate;
}
@Override
public Type getType() {
return type;
}
@Override
public Date getCreatedOnDate() {
return DateConverter.stringToDate(creationDate);
}
@Override
public Date getLastUpdateOnDate() {
return DateConverter.stringToDate(lastUpdate);
}
@Override
public String getCreatedBy() {
return null;
}
@Override
public String getLastUpdatedBy() {
return null;
}
@Override
public List<String> getAccessList() {
return null;
}
@Override
public void grantAccess(User user) {
}
public String getName() {
return name;
}
public String getPostalCode() {
return postalCode;
}
public List<String> getInstructionProviderIds() {
return instructionProviderIds;
}
public List<String> getAdminIds() {
return adminIds;
}
public String getCreationDate() {
return creationDate;
}
public final class User implements Fetchable {
public static final String KEY = "user";
private String remoteId;
private String email;
private String displayName;
private String creationDate;
private String lastUpdate;
private String createdBy;
private String lastUpdatedBy;
private List<String> accessList;
@Ignore
private final Fetchable.Type type = Type.USER;
private User() {
accessList = new ArrayList<>();
}
public User(String remoteId, String email, String displayName, String creationDate, String lastUpdate, String createdBy, String lastUpdatedBy, List<String> accessList) {
this.remoteId = remoteId;
this.email = email;
this.displayName = displayName;
this.creationDate = creationDate;
this.lastUpdate = lastUpdate;
this.createdBy = createdBy;
this.lastUpdatedBy = lastUpdatedBy;
this.accessList = accessList;
}
public static String getID() {
return KEY;
}
public String getRemoteId() {
return remoteId;
}
public void setRemoteId(String remoteId) {
this.remoteId = remoteId;
}
@Override
public Type getType() {
return type;
}
@Override
public Date getCreatedOnDate() {
return DateConverter.stringToDate(creationDate);
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getCreationDate() {
return creationDate;
}
public String getLastUpdate() {
return lastUpdate;
}
@Override
public Date getLastUpdateOnDate() {
return DateConverter.stringToDate(lastUpdate);
}
@Override
public String getCreatedBy() {
return null;
}
@Override
public String getLastUpdatedBy() {
return null;
}
@Override
public List<String> getAccessList() {
return null;
}
@Override
public void grantAccess(User user) {
}
【问题讨论】:
标签: android firebase google-cloud-firestore