- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我将应用程序发布的想法存储在 Firestore 中。数据像这样存储在 Firestore 中 Ideas/{documentID}/IdeaObject。问题是当我检索数据时,它没有按发布时间排序。检索到的想法是根据 Firestore 自动创建的 documentID 的 id。我在我的模型类中使用了 ServerTimestamp 并且当我检索它时,我将 orderBy
方法与我的 Firestore 引用一起使用,但仍然没有。
想法.java
public class Idea {
@ServerTimestamp
private Date date;
private String title, idea, timeCommitment, ideaStage, postedBy, website, videoPitch;
private int views, favorites;
private ArrayList<String> lookingFor = new ArrayList<>();
private ArrayList<String> tags = new ArrayList<>();
private String userID;
private String timeStamp;
public Idea() {
}
public Idea(String title, String idea, String timeCommitment, String ideaStage, String postedBy, String website, String videoPitch, int views, int favorites, ArrayList<String> lookingFor, ArrayList<String> tags, String userID, String timeStamp) {
this.title = title;
this.idea = idea;
this.timeCommitment = timeCommitment;
this.ideaStage = ideaStage;
this.postedBy = postedBy;
this.website = website;
this.videoPitch = videoPitch;
this.views = views;
this.favorites = favorites;
this.lookingFor = lookingFor;
this.tags = tags;
this.userID = userID;
this.timeStamp = timeStamp;
}
想法发布方法
private void postIdea() {
final String ideaID = UUID.randomUUID().toString();
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Idea ideas = new Idea(title, idea, timeCommitment, ideaStage, AppValues.fullName, website, videoPitch, 0, 0, lookingFor, tags, AppValues.userId, "" + timestamp.getTime());
firestoreDb.collection("ideas")
.document(ideaID)
.set(ideas)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
postIdeaUser(ideaID);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
hideLoadingDialog();
showToast(e.getMessage());
}
});
}
按时间检索所有想法
firestoreDb.collection("ideas")
.orderBy("timeStamp", Query.Direction.ASCENDING)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
ideaArrayList = new ArrayList<>();
ideaArrayList.clear();
for (DocumentSnapshot documentSnapshot : task.getResult()) {
Idea idea = documentSnapshot.toObject(Idea.class);
if (!idea.getUserID().equals(AppValues.userId)) {
ideaArrayList.add(idea);
}
}
callAdapter();
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
progressBar.setVisibility(View.GONE);
swipeRefresh.setEnabled(true);
errorText.setVisibility(View.VISIBLE);
}
}
});
我想要实现的是检索所有想法并让它们按 TimeStamp 值升序排列。
最佳答案
在查询数据库时,您不能使用字符串 (timeStamp
) 而不是日期 (date
) 并期望其行为与日期相同。所以要解决这个问题,请更改以下代码行:
firestoreDb.collection("ideas")
.orderBy("timeStamp", Query.Direction.ASCENDING)
到
firestoreDb.collection("ideas")
.orderBy("date", Query.Direction.ASCENDING)
要使其工作,这种查询需要一个索引。要创建一个,请查看我在以下帖子中的回答:
关于java - 按时间戳按升序排列 Firestore 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50224638/
我正在尝试为我的 Firestore 设置一个数据库,但是我尝试重新安装 pod 和许多其他东西,但我仍然无法让它工作,因为它显示了这个错误: Type 'Firestore' has no memb
我需要更改我的项目 ID,因为要验证的 Firebase 身份验证链接在链接上显示了项目 ID,并且由于品牌 reshape ,项目名称已更改。根据我发现的信息,更改项目 ID 似乎不太可能。我正在考
快速提问。长话短说,我在我的谷歌云功能日志中收到此错误: Firestore (4.10.1):无法到达 Firestore 后端。 这是我的函数文件中的代码: // pull in firebas
我正在从事 Angular 6 项目。这是我使用 --prod 构建时遇到的错误标记、主持和运行。我已经坐了很长时间了。最初认为这可能是 firestore 包的问题,我等了。但是现在更新到fir
我正在开发一个 React 项目,这是我的第一个 React 项目。此代码部署成功。但在使用 postman 测试时出现一些错误。我“发布”“createScream”函数的 URL 并发送它。然后我
我有一个包含两个集合的 Firestore 数据库:用户和锦标赛。用户具有“参与者”角色和“管理员”角色,并在用户文档中由“isParticipant”和“isAdmin” bool 值指示: /us
Firebase 数据库根据他们的文档提供了 10 MB 的离线数据库缓存限制,但没有提到 的离线数据限制。 Firestore 数据库。 Firestore 的离线数据保存限制是多少? 最佳答案 根
我正在尝试评估 string在 Firestore 安全规则 基于 matches正则表达式功能 我的代码是 username.matches('^(?!\.)(?!_)(?!.*\.$)(?!.*?
是否可以在 Firestore 中定义具有唯一约束的索引?如果没有,如何在文档字段上强制执行唯一性(不使用文档 ID)? 最佳答案 是的,这可以通过结合使用两个集合、Firestore 规则和批量写入
我正在学习 GCP,在他们的 Firestore 中,我对 Admin.firestore 和 Firebase.firestore 的区别感到困惑。 这是管理员的代码: const admin =
使用带有自定义声明的 firestore 在线安全模拟会导致错误,但它在部署时可以完美运行(同时实际处理真实请求)。错误是: Error: simulator.rules line [5], colu
所以,我知道有一些类似命名的问题,但这是不一样的。 我很想知道是否有人可以解释缺少 increment 的原因。哨兵,类似于delete一。 据我所知,字段删除与文档更新没有什么不同。意思是,我只能
我想创建两个带有分页选项的查询。在第一个记录中,我想获取前十条记录,在第二个记录中,我想获取其他所有记录: .startAt(0) .limit(10) .startAt(9) .limit(null
我正在努力为我的应用寻找最佳架构。我应该使用顶级集合、子集合、数组等吗? 设置: 我的应用程序将有许多用户将参与的测验。 每个测验都会有多个问题。 每个问题都有多个答案,只能选择一个。 每个用户只能回
我正在努力为我的应用寻找最佳架构。我应该使用顶级集合、子集合、数组等吗? 设置: 我的应用程序将有许多用户将参与的测验。 每个测验都会有多个问题。 每个问题都有多个答案,只能选择一个。 每个用户只能回
我无法在任何地方找到我可以在一个Collection中获得的文档数量的限制。假设我有1,000,000,000个文档...那有可能吗?如果我想把它们全部都拿走,实际上会给我十亿吗? 最佳答案 可以存储
假设我有一个集合 mycollection有 1,000,000 条记录。 此查询将返回多少条记录? const query = firestore.collection('mycollection'
这是错误消息:@firebase/firestore: Firestore (4.12.1): Could not reach Firestore backend 我正在构建一个网络应用程序,它今天运
我在编写和测试我的 Firestore 规则时遇到了一个奇怪的问题。这是我想要实现的目标: 当应用程序启动时,用户会匿名登录。这用户开始新游戏。 我创建了一个基本上只包含时间戳的“ session ”
我是云函数的新手。我有一些困惑。 admin.firestore 和functions.firestore? admin.database 是实时数据库吗? 因此,如果云函数基本上是用 JavaScr
我是一名优秀的程序员,十分优秀!