Could not complete scheduled request to refresh en

2019-04-07 16:20发布

问题:

Let me get straight to the point here the error in the logcat is:

Could not complete scheduled request to refresh entries. ClientErrorCode: 3

I have tested the Realm() part of the code and it fetched the right data. Basically, the app just crashes when it loads that Activity. All Im trying to do right now is post the itemName in each cell. If you guys need the logcat, just say so and I'll post it. Any other details needed too.

This is the code for my Activity with a recyclerView with just an ImageView and a TextView in each cell.:

class EssentialsActivity : AppCompatActivity() {

    var category: String? = null
    val realmtypeFunctions = RealmTypeFunctions()
    var realmResults: RealmResults<ChattRItem>? = null
    var chattRItemList = mutableListOf<ChattRItem>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_essentials)

        //init realm
        Realm.init(this)

        category = "People"

        recyclerView_Essentials.setBackgroundColor(Color.CYAN)
        recyclerView_Essentials.layoutManager = GridLayoutManager(this, 3)
//        AsyncTask.execute {
            category?.let {
                loadFromRealm(it)
            }
//        }
        this.runOnUiThread {
            recyclerView_Essentials.adapter = EssentialsAdapter(chattRItemList)
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        val inflater = menuInflater
        inflater.inflate(R.menu.categories, menu )

        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        val intent: Intent?
        intent = Intent(this, AddItemActivity::class.java)
        intent.putExtra("category", category)
        startActivity(intent)


//        when (item?.itemId) {
//            R.id.essentials_menu_item -> {
//                intent = Intent(this, EssentialsActivity::class.java)
//                startActivity(intent)
//            }
//            R.id.addItem_menu_item -> {
//                intent = Intent(this, AddItemActivity::class.java)
//                startActivity(intent)
//            }
//            else -> return false
//        }

        return super.onOptionsItemSelected(item)
    }

    private fun loadFromRealm(category: String){
        val realm = Realm.getDefaultInstance()
        try {
            val query: RealmQuery<ChattRItem>? = realm.where(ChattRItem::class.java).equalTo("itemCategory", category)
            val result: RealmResults<ChattRItem>? = query?.findAll()
            result?.let {
                for (i in it) {
                    println(i.itemName)
                    chattRItemList.add(i)
                }
                println(chattRItemList.count())
            }

        } finally {
            realm.close()
        }
    }
}

class EssentialsAdapter(private val chattRItemList: List<ChattRItem>): RecyclerView.Adapter<CustomViewHolder>(){

    //realm class variable here to be displayed
    /* var essentials = array of realm essential item */
//    var essentialsActivity = EssentialsActivity()

    //number of items
    override fun getItemCount(): Int {
//        return 12 //return realm items count
        return this.chattRItemList.size
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {

//        holder.itemView.textView_essentials_name.text = "Essentials Item"

        val chattRItem = chattRItemList.get(position)
//        holder.itemView.textView_essentials_name.text = chattRItem.itemName
        holder.bind(chattRItem)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder{
        // how do we create a cell view
        val layoutInflater = LayoutInflater.from(parent.context)
        val cellForRow = layoutInflater.inflate(R.layout.essentials_cells_layout, parent, false)
        return CustomViewHolder(view = cellForRow)
    }

}

class CustomViewHolder(view: View): RecyclerView.ViewHolder(view) {
    fun bind(chattRitem: ChattRItem) {
        itemView.textView_essentials_name.text = chattRitem.itemName
    }
}

回答1:

So basically I figured it out. This was not the right error from LogCat. There was another set of errors from Logcat many lines above this. The error was the result list was a @Realm object. My recyclerView was asking for a non RealmClass object. So i had to make a similar object except not a RealmClass.

@RealmClass
open class ChattRItem: RealmModel {
    @PrimaryKey var itemId: String = ""
    var itemName: String = ""
    var itemCategory: String = ""
    var itemImageFileName: String = ""
    var itemAudioFileName: String = ""
}

class ChattRBoxItems(val itemId: String, val itemName: String, val itemCategory: String, val itemImageFileName: String, val itemAudioFileName: String)

then I mapped the result into this new class then applied it to my recyclerView.