startAfter()方法使应用程序Firestore数据库崩溃

时间:2019-07-10 06:18:21

标签: android firebase google-cloud-firestore firebase-security-rules

第一次数据正确,但是下次应用崩溃并出现以下错误

依赖性

implementation 'com.google.firebase:firebase-firestore-ktx:20.1.0'

崩溃详细信息:

 --------- beginning of crash
2019-07-10 11:38:39.184 14145-14145/com.ycrathi.surajgold E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ycrathi.surajgold, PID: 14145
    java.lang.RuntimeException: No properties to serialize found on class com.google.firebase.firestore.A
        at com.google.firebase.firestore.g.k$a.<init>(:677)
        at com.google.firebase.firestore.g.k.a(:365)
        at com.google.firebase.firestore.g.k.m(:177)
        at com.google.firebase.firestore.g.k.b(:104)
        at com.google.firebase.firestore.g.k.a(:77)
        at com.google.firebase.firestore.H.b(:210)
        at com.google.firebase.firestore.H.a(:200)
        at com.google.firebase.firestore.z.a(:687)
        at com.google.firebase.firestore.z.a(:524)
        at d.f.a.d.b.q.a(:134)
        at androidx.recyclerview.widget.RecyclerView.d(:4961)
        at androidx.recyclerview.widget.RecyclerView$w.run(:5117)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
        at android.view.Choreographer.doCallbacks(Choreographer.java:761)
        at android.view.Choreographer.doFrame(Choreographer.java:693)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7045)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

我阅读了这个问题:How to paginate Firestore with Android? 应用了此链接https://stackoverflow.com/a/50742175/5096868

中可用的相同逻辑

代码:

class CreditListFragment : BaseFragment() {

private lateinit var db: FirebaseFirestore
private lateinit var collectionReference: CollectionReference
private lateinit var binding: CreditListFragmentBinding
//private lateinit var adapterTxnEntry: CreditEntryRVAdapter
private var isScrolling = false
private var isLastItemReached = false
private val pageLimit: Long = 10
private var lastVisible: DocumentSnapshot? = null

companion object {
    fun newInstance() = CreditListFragment()
}

private lateinit var viewModel: CreditEntryViewModel

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    binding = DataBindingUtil.inflate(
        inflater, R.layout.credit_list_fragment, container, false
    )
    viewModel = ViewModelProviders.of(this).get(CreditEntryViewModel::class.java)
    binding.creditEntryVM = viewModel

    return binding.root
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    //setUpList()
    db = FirebaseFirestore.getInstance()
    collectionReference = db.collection(BuildConfig.CREDIT_ENTRY_COLLECTION)
    val baseQuery = collectionReference.orderBy("lastUpdated", Query.Direction.DESCENDING).limit(pageLimit)
    //setUpAdapter()
    val arrayList = ArrayList<AddEntry>()

    binding.transactionRecyclerView.layoutManager = LinearLayoutManager(activity)
    val adapter = CreditListRVAdapter(activity, arrayList)
    binding.transactionRecyclerView.adapter = adapter

    binding.swipeRefreshLayout.isRefreshing = true

    baseQuery.get().addOnCompleteListener {
        if (it.isSuccessful) {
            it.result?.documents?.forEach {
                val addEntry = it.toObject(AddEntry::class.java)
                if (addEntry != null) {
                    addEntry.id = it.id
                    arrayList.add(addEntry)
                    lastVisible = it
                    LogUtils.d("Yogesh","Added entry " + lastVisible)
                }
            }
            binding.swipeRefreshLayout.isRefreshing = false
            adapter.notifyDataSetChanged()

            if (it.result?.size()!! < pageLimit) {
                isLastItemReached = true
            }

            if (arrayList.size == 0) {
                showNoDataAvailableDialog(
                    "Great..!!",
                    "There is no any credit in our database!!"
                )
            }
        }else {
            showNoDataAvailableDialog(
                "Error..!!",
                "Please check your internet connection!!"
            )
        }
    }


    val onScrollListener = object : RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            super.onScrollStateChanged(recyclerView, newState)
            if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                isScrolling = true
            }
        }

        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)

            val linearLayoutManager = recyclerView.layoutManager as LinearLayoutManager?
            var firstVisibleItemPosition = linearLayoutManager?.findFirstVisibleItemPosition()
            var visibleItemCount = linearLayoutManager?.childCount
            val totalItemCount = linearLayoutManager?.itemCount

            if (visibleItemCount == null)
                visibleItemCount = 0

            if (firstVisibleItemPosition == null)
                firstVisibleItemPosition = 0

            if (isScrolling && firstVisibleItemPosition + visibleItemCount == totalItemCount && !isLastItemReached) {
                isScrolling = false

                LogUtils.d("Yogesh", "OnScrolling $isScrolling")
                /*val nextQuery = collectionReference.orderBy("lastUpdated", Query.Direction.DESCENDING)
                .startAfter(lastVisible)
                .limit(pageLimit)*/

                LogUtils.d("Yogesh", "Set query $isScrolling")
                baseQuery.startAfter(lastVisible)
                baseQuery.get().addOnCompleteListener {
                    try {
                        LogUtils.d("Yogesh","Inside complete "+it.isSuccessful )
                        if (it.isSuccessful) {
                            it.result?.documents?.forEach { it1 ->
                                val addEntry = it1.toObject(AddEntry::class.java)
                                if (addEntry != null) {
                                    addEntry.id = it1.id
                                    arrayList.add(addEntry)
                                    lastVisible = it1
                                    LogUtils.d("Yogesh", "Second Added entry $lastVisible")
                                }
                            }
                            adapter.notifyDataSetChanged()
                        }

                        if (it.result?.size()!! < pageLimit) {
                            isLastItemReached = true
                        }
                    }catch (e : Exception){
                        e.printStackTrace()
                        LogUtils.d("Yogesh", "Error ${e.message}")
                    }
                }.addOnFailureListener{
                    LogUtils.d("Yogesh", "Error ${it.message}")
                }
            }
        }
    }
    binding.transactionRecyclerView.addOnScrollListener(onScrollListener)

    binding.swipeRefreshLayout.setOnRefreshListener {
        binding.swipeRefreshLayout.isRefreshing = false
    }
}
}

AddEntry类

 @Keep
 class AddEntry : Serializable {


@Exclude
var id = ""

/*
* total amount
* */
@SerializedName("t_amt")
var t_amt: Float = 0.0f

/*
* Credit Amount
* */
@SerializedName("c_amt")
var c_amt: Float = 0.0f

/*
* Customer mobile
* */

@SerializedName("cMob")
var cMob: String = ""

/*
* Customer Name
* */

@SerializedName("cName")
var cName: String = ""


/*
* moderator name
* */
@SerializedName("mName")
var mName: String = ""

/*
* moderator mobile
* */
@SerializedName("mMob")
var mMob: String = ""

/*
* Entry time
* */
@SerializedName("createdAt")
var createdAt: Long = 0

/*
* Updated time
* */
@SerializedName("lastUpdated")
var lastUpdated: Long = 0

/*
* reminder date
* */
@SerializedName("reminderDate")
var reminderDate: Long = 0


override fun toString(): String {
    return "\n[" +
            "t_amt : $t_amt,\n" +
            "c_amt : $c_amt,\n" +
            "cMob : $cMob,\n" +
            "cName : $cName,\n" +
            "mName : $mName,\n" +
            "mMob : $mMob,\n" +
            "reminderDate : $reminderDate,\n" +
            "createdAt : $createdAt\n" +
            "lastUpdated : $lastUpdated\n" +
            "]"
}
}

实际上我使用的是 FirestorePagingAdapter ,但是如果我们使用它,那么我们将无法更新适配器,我想更新项目。 如果有问题,请协助我。

设备:三星s8,三星s9

数据库结构:

--credit_entry_collection (collection)
           -- unique_document_id (document)
                 --AddEntry model class

enter image description here

重要提示:如果我删除startAfter()方法,则可以正常工作。 但是分页不起作用

1 个答案:

答案 0 :(得分:-1)

在Firebase实时数据库中(在浏览器中)将规则更改为公共