是否可以刷新委派创建的LiveData?

时间:2019-12-04 15:07:48

标签: android kotlin

我有LiveData,它是通过委派创建的,它是从REST API服务加载的对象列表。现在,我想刷新该列表(向新列表请求api)。这是我的代码

class SomeViewModel(itemsRespository: ItemsRepository): ViewModel(){

    val items by ItemsDelegate(itemsRepository)

    fun refreshItems(){
        //todo refresh items
    }
}

1 个答案:

答案 0 :(得分:3)

  1. 您无法更新private Bonjour.DNSSDEventManager eventManager = new DNSSDEventManager(); // subscribed to ServiceFound, ServiceLost, QueryRecordAnswered, OperationFailed events private Bonjour.DNSSDService service = new DNSSDService(); private Bonjour.DNSSDService resolver = null; private void QueryRecord() { try { resolver = service.QueryRecord(0, 0, "_ourservice._tcp.local", DNSSDRRType.kDNSSDType_PTR, DNSSDRRClass.kDNSSDClass_IN, eventManager); } catch (Exception ex) { } } private void QueryAnswered( // event handler for QueryRecordAnswered DNSSDService service, DNSSDFlags flags, uint ifIndex, string fullname, DNSSDRRType rrtype, DNSSDRRClass rrclass, object rdata, uint ttl) { resolver.Stop(); resolver = null; try { // able to reach this event handler without problems service.ReconfirmRecord(flags, ifIndex, fullname, DNSSDRRType.kDNSSDType_PTR, DNSSDRRClass.kDNSSDClass_IN, rdata); } catch (Exception ex) { // throws HRESULT: 0xFFFEFFFF } } // clean up resources when finished 。您将需要LiveData。因此,请确保MutableLiveData返回ItemsDelegate(itemsRepository)

  2. 致电MutableLiveDataitems.value = <new value>。更新实时数据的价值。