0

Background - I have an app that currently has a lot of methods in MainActivity to pull and upload data to firebase. It's quite a mess with the other app logic in there..

Problem - I would like a separate class filled with server (firebase) access methods that can be called from any activity or fragment and run asynchronously. The class also needs access to context and be able to initialise and store variables.

PS. I'm using kotlin if that helps at all

Edit: Have done more researching to find terms like "utility" and "static" classes which seems like an accurate way to go... If I create a static utility class with Async methods, will that achieve what I'm after? Also, can you initialise and hold variables in a static class?

Thanks :)

Solved After more research and testing, I created an object that holds my methods and variables and just need to pass context into the relevant methods. Then just simply call objectname.methodname()

4
  • I don't know if this helps you but, usually, AsyncTask is used for doing server related transactions in the background. Check out this link. Commented Jul 23, 2018 at 19:40
  • Thanks Nandan. I know a little bit about AsyncTask and Anko, and will be using them for my server transactions.. I just don't know how to have a whole class of methods using AsyncTask, or know how to call each such method from different fragments. Should they be in an object and then I just pass context into the method parameters?? Commented Jul 23, 2018 at 20:39
  • Firebase methods should already be asynchronous. Please show your code and maybe someone can help more specifically to your question Commented Jul 24, 2018 at 2:54
  • Thanks cricket. I didn't actually know that but asynchronous methods aren't really bothering me at the moment. I'd just like a class that doesn't need to be instantiated to just hold my firebase access methods. Commented Jul 24, 2018 at 2:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.