android - 请求数据异步耗时操作如何返回?
巴扎黑
巴扎黑 2017-04-17 17:31:24
0
6
684
public List<QiangYu> load() {
    BmobQuery<QiangYu> query = new BmobQuery<QiangYu>();

    query.findObjects(UiUtils.getContext(), new FindListener<QiangYu>() {

        @Override
        public void onSuccess(List<QiangYu> list) {
            //这个list是不为空的.我想问的是如何把这个list数据在load方法中返回出去.
            //因为耗时.这样直接返回是null.
            
            } 
        }

        @Override
        public void onError(int arg0, String arg1) {
        };
    });

    //因为onSuccess耗时.这样写,会直接返回null
    //有什么办法,可以让onSuccess完成,获取好数据在返回???
    return list;
}
巴扎黑
巴扎黑

reply all(6)
洪涛

Asynchronous requests should not be returned like this, callbacks should be used.
Write an interface, call the interface method in onSuccess, and then call back when using it

伊谢尔伦

It is recommended that you first learn the link description of callbacks

PHPzhong

As mentioned above, asynchronous tasks are implemented using interfaces, and it is best to deal with thread issues when calling interface methods.

伊谢尔伦

rxjava is a good choice

小葫芦

I don’t understand why you need to repackage it. In fact, Bmob is already packaged. If you need it, just write an interface callback

public getListListener gll;
//接口
public interface getListListener{
    void onSucceed(List<data>);
}

public void load(final getListListener gll){
    
    this.gll=gll;
    ...
    ...
    Bmob.find(new findListener(){
    
        void onSucceed(List<data> data){
            //回调方法
            gll.onsucceed(data);
        }
        ...
    });
}

//使用:
load(new getListListener(){
    void onSuccees(list<data> data){
        //得到数据
        //写你自己的逻辑.
    }
});

Passing above. . . You'll find it's of no use. . . So just use the Bmob callback interface for the interface! No need to do anything unnecessary! !

小葫芦

1. Perform time-consuming operations in asynchronous threads
2. After the query is completed, you can use EventBus or RxBus to send messages to the main thread
3. The main thread accepts events and then refreshes# 🎜🎜# or
Use the RxJava
mentioned above

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template