简体   繁体   English

不明白为什么AsyncTask给我一个不兼容的类型

[英]Not understanding why AsyncTask is giving me an incompatible type

I'm trying to get access into my database in the background so that i don't lock the threads and get an error, so i read i should use AsyncTask. 我试图在后台访问我的数据库,以便不锁定线程并收到错误,所以我读到我应该使用AsyncTask。 from what i read up on it takes uses 3 data types. 从我阅读的内容来看,它使用3种数据类型。 The data type it takes in, the data type it processes, and the return data type. 它接受的数据类型,它处理的数据类型以及返回数据类型。 so here im making a step tracker and want to access my database to get something by id, so i call my repository and pass in the database i'm using and the id i want to find 所以我在这里制作了一个步进跟踪器,想通过id访问我的数据库,以获取某些东西,所以我调用存储库,并传入我正在使用的数据库和要查找的id

cStep = stepRepository.getStepById(stepDatabase,0);

and this here is my repository class and the AsyncTask within it 这是我的存储库类和其中的AsyncTask

> public class StepRepository implements IStepDataSource {
        private IStepDataSource mLocalDataSource;
        private static StepRepository mInstance;


public StepRepository(IStepDataSource mLocalDataSource) {
    this.mLocalDataSource = mLocalDataSource;
}

public static StepRepository getInstance(IStepDataSource mLocalDataSource){
    if(mInstance == null)
        mInstance = new StepRepository(mLocalDataSource);

    return mInstance;
}

@Override
public Flowable<List<Step>> getAllSteps() {
    return mLocalDataSource.getAllSteps();
}

@Override
public Step getStepById(StepDatabase db, int userId) {
    return new getAsyncTask(db).execute(userId);
}


private static class getAsyncTask extends AsyncTask<Integer, Void, Step> {

    getAsyncTask(StepDatabase db) {
        this.db = db;
    }

    @Override
    protected Step doInBackground(Integer... params) {
        StepDao dao = db.stepDao();
        return dao.getStepById(params[0]);
    }

    @Override
    protected void onPostExecute(Step step) {

    }
}

@Override
public void insertStep(Step... steps) {
    mLocalDataSource.insertStep(steps);
}

@Override
public void updateStep(Step... steps) {
    mLocalDataSource.updateStep(steps);
}

@Override
public void deleteStep(Step step) {
    mLocalDataSource.deleteStep(step);
}

} }

im not getting why getUserByid is giving me imcopatible type since AsyncTask takes in and interger and returns a steps which is what i want?? 即时通讯不明白为什么getUserByid给我不兼容的类型,因为AsyncTask接受并进行整数运算,并返回我想要的步骤?

btw if its any help this is the IStepDataSource my repository implements 顺便说一句,如果有帮助,这是我的存储库实现的IStepDataSource

public interface IStepDataSource {

Flowable<List<Step>> getAllSteps();
Step getStepById(StepDatabase db, int userId);
void insertStep(Step... steps);
void updateStep(Step... steps);
void deleteStep(Step step);

} }

The execute() method of AsyncTask returns void and your are attempting to return a void from a method declared as returning Step. AsyncTask的execute()方法返回void,而您正在尝试从声明为返回Step的方法中返回void。 It's probably best to get the AsyncTask out of the getStepById() method and instead use an AsyncTask where you invoke getStepById(). 最好是从getStepById()方法中获取AsyncTask,而在调用getStepById()的地方使用AsyncTask。 I think you're assuming that execute() blocks until the task is complete and that is incorrect. 我认为您假设execute()阻塞,直到任务完成,这是不正确的。 If this were the case, there'd be no point to using AsyncTask. 如果真是这样,那就没有必要使用AsyncTask了。 execute() returns immediately and onPostExecute(Step step) is where the results should be processed/displayed/whatever. execute()立即返回,onPostExecute(Step step)是应该处理/显示/以任何方式显示结果的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM