Not understanding why AsyncTask is giving me an incompatible type










0














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. from what i read up on it takes uses 3 data types. 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



cStep = stepRepository.getStepById(stepDatabase,0);


and this here is my repository class and the AsyncTask within it



> 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??



btw if its any help this is the IStepDataSource my repository implements



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);











share|improve this question

















  • 2




    You've misunderstood the usage of it, please see stackoverflow.com/questions/9671546/asynctask-android-example
    – TWL
    Nov 12 at 20:14















0














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. from what i read up on it takes uses 3 data types. 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



cStep = stepRepository.getStepById(stepDatabase,0);


and this here is my repository class and the AsyncTask within it



> 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??



btw if its any help this is the IStepDataSource my repository implements



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);











share|improve this question

















  • 2




    You've misunderstood the usage of it, please see stackoverflow.com/questions/9671546/asynctask-android-example
    – TWL
    Nov 12 at 20:14













0












0








0







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. from what i read up on it takes uses 3 data types. 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



cStep = stepRepository.getStepById(stepDatabase,0);


and this here is my repository class and the AsyncTask within it



> 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??



btw if its any help this is the IStepDataSource my repository implements



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);











share|improve this question













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. from what i read up on it takes uses 3 data types. 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



cStep = stepRepository.getStepById(stepDatabase,0);


and this here is my repository class and the AsyncTask within it



> 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??



btw if its any help this is the IStepDataSource my repository implements



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);








android multithreading android-studio asynchronous android-asynctask






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 19:41









cooldude22

176




176







  • 2




    You've misunderstood the usage of it, please see stackoverflow.com/questions/9671546/asynctask-android-example
    – TWL
    Nov 12 at 20:14












  • 2




    You've misunderstood the usage of it, please see stackoverflow.com/questions/9671546/asynctask-android-example
    – TWL
    Nov 12 at 20:14







2




2




You've misunderstood the usage of it, please see stackoverflow.com/questions/9671546/asynctask-android-example
– TWL
Nov 12 at 20:14




You've misunderstood the usage of it, please see stackoverflow.com/questions/9671546/asynctask-android-example
– TWL
Nov 12 at 20:14












1 Answer
1






active

oldest

votes


















1














The execute() method of AsyncTask returns void and your are attempting to return a void from a method declared as returning Step. It's probably best to get the AsyncTask out of the getStepById() method and instead use an AsyncTask where you invoke getStepById(). I think you're assuming that execute() blocks until the task is complete and that is incorrect. If this were the case, there'd be no point to using AsyncTask. execute() returns immediately and onPostExecute(Step step) is where the results should be processed/displayed/whatever.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269023%2fnot-understanding-why-asynctask-is-giving-me-an-incompatible-type%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The execute() method of AsyncTask returns void and your are attempting to return a void from a method declared as returning Step. It's probably best to get the AsyncTask out of the getStepById() method and instead use an AsyncTask where you invoke getStepById(). I think you're assuming that execute() blocks until the task is complete and that is incorrect. If this were the case, there'd be no point to using AsyncTask. execute() returns immediately and onPostExecute(Step step) is where the results should be processed/displayed/whatever.






    share|improve this answer

























      1














      The execute() method of AsyncTask returns void and your are attempting to return a void from a method declared as returning Step. It's probably best to get the AsyncTask out of the getStepById() method and instead use an AsyncTask where you invoke getStepById(). I think you're assuming that execute() blocks until the task is complete and that is incorrect. If this were the case, there'd be no point to using AsyncTask. execute() returns immediately and onPostExecute(Step step) is where the results should be processed/displayed/whatever.






      share|improve this answer























        1












        1








        1






        The execute() method of AsyncTask returns void and your are attempting to return a void from a method declared as returning Step. It's probably best to get the AsyncTask out of the getStepById() method and instead use an AsyncTask where you invoke getStepById(). I think you're assuming that execute() blocks until the task is complete and that is incorrect. If this were the case, there'd be no point to using AsyncTask. execute() returns immediately and onPostExecute(Step step) is where the results should be processed/displayed/whatever.






        share|improve this answer












        The execute() method of AsyncTask returns void and your are attempting to return a void from a method declared as returning Step. It's probably best to get the AsyncTask out of the getStepById() method and instead use an AsyncTask where you invoke getStepById(). I think you're assuming that execute() blocks until the task is complete and that is incorrect. If this were the case, there'd be no point to using AsyncTask. execute() returns immediately and onPostExecute(Step step) is where the results should be processed/displayed/whatever.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 21:08









        Greg Moens

        33118




        33118



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269023%2fnot-understanding-why-asynctask-is-giving-me-an-incompatible-type%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            Evgeni Malkin