Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Murach's Android Programming: AsyncTask class for CRUD

 
Jhon Barrantes
Greenhorn
Posts: 17
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joel,

Congrats on your new book!

The first edition of your book was the first book on Android I read and I'm glad I chosen your book because it was very easy to follow and gave a general overview of the main components of the android OS, which helped me a lot when I read more advanced books.

In all the books I have read I haven't been able to find a general way to perform CRUD operation off the main thread, they all recommend not to execute those operations on the main thread, but they either do not show any example of how to do that or simply use the AsyncTask class which can be pretty tedious and repetitive when you have to write several of those operations in the same Activity.

What pattern/class would you recommend to use so that we avoid having to write a new AsyncTask class for every CRUD operation we need to perform?

Thanks.
 
Joel Murach
Author
Ranch Hand
Posts: 77
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jhon,

Thanks for the kind words about the first edition of our book. It makes my day to hear that you liked it. The second edition is mostly the same, though I think we made a few minor improvements. The only major change is switching the IDE from Eclipse to Android.

What kind of CRUD operations are you talking about? Are you updating data in the cloud or in a local SQLite database? If you're updating data in a local SQLite database, I think you can use the main thread. If you're updating data in the cloud, I don't know of any better pattern/class than the AsyncTask class.

Is it possible to keep a local copy of the data to allow you to synchronize with the cloud less often? Maybe that would help.

Thanks,

Joel
 
Jhon Barrantes
Greenhorn
Posts: 17
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joel,

Thank you for taking the time to reply.

Regarding the CRUD operations, well I was basically talking about a local SQLite db. I do update data in the cloud, but I guess there's plenty of options when it comes to doing that : IntentServices, Jobscheduler, SyncAdapter, etc.
It's in those apparently trivial CRUD operations that we are left on our own Almost all the books I have read mentioned that we should avoid to perform such operations off the main thread.
For instance, here I'm quoting one of the most extensive books on Android, "The Busy Coder's Guide To Android Development":

You really should do your database I/O on background threads. Opening a database is cheap, but working with it (queries, inserts, etc.) is not.


The problem is that despide all those recommendations, none of those books give a clear idea of how to do what they recommend. I know that when we're taking our first steps into the Android FW, we tend to use the AsyncTask for almost everything, but on further reading I came to the conclusion that I should stay away from that class, for many reasons, mainly because coordinating long-running tasks over the Activity lifecycle can be difficult and memory leaks can result if you aren't careful.

For that reason I always wanted to have the chance to ask a professional developer/author how they handle that kind of operations in a real-world application.
 
Joel Murach
Author
Ranch Hand
Posts: 77
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read a half dozen Android books, and I have yet to see the recommendation that you should use a background thread for CRUD operations on a local database. So, I'm not sure all developers would recommend this.

In the end, I suppose it depends on how long it takes the database operations for your particular app to execute, which depends on many factors. I would start by using the main thread for CRUD operations and testing your app to see if these operations cause problems with your app. If they don't, you don't need to bother with a background thread. If they do, you need to continue your search for a better solution than the AsyncTask class. Unfortunately, if you need to run your CRUD operations on a background thread, I don't know of an elegant solution.

Maybe somebody else out there on the Ranch does? Maybe some other Android programmers would like to weigh in on this issue?
 
Jhon Barrantes
Greenhorn
Posts: 17
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well to tell you the truth, this is one of the things about Android that has caused me more headaches. For that reason I kept digging and found out that there are actually two APIs that can really help when it comes to CRUD operation but they only work with ContentProviders:

  • CursorLoader
  • AsyncQueryHandler


  • Since I got familiar with those classes I started using Content Providers in almost all of my projects, except for a few ones where I didn't consider necessary to go through the hassle of implementing a Content Provider. And it was for those cases that I was trying to find an alternative way to execute SQLite operations
     
    Joel Murach
    Author
    Ranch Hand
    Posts: 77
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well, you obviously have more experience with this than me. I just read this:

    http://www.grokkingandroid.com/using-loaders-in-android/

    It's a pretty good explanation of using the CursorLoader API with a content provider, which is what you've been doing. But, if you scroll down, to the heading that says "Using Loaders to access your SQLiteDatabase" it talks about an extension to this API that works without a content provider. That seems like it might be what you're looking for, though maybe you've already looked into this and determined that is isn't.

    As for me, I'm going to consider adding coverage of Loaders to the next edition of my book. In my experience, one of the best things about being here on the Ranch is that I usually learn something that I can use to improve my books.

    Thanks!
     
    Jhon Barrantes
    Greenhorn
    Posts: 17
    2
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yeah that guy is really awesome and very kind. He helped me a lot when I started using Content Providers.
    Regarding the use of CursorLoaders without a ContentProvider, well a couple of years ago the library Wolfram(the blog's author) mentioned in his articule was very popular but the project is discontinued now.
    We can always implement our own loaders, but this requires both more work and a deeper insight into the framework.

    I'm glad you're considering adding a chapter of Loaders to the next edition of your book. On that same note, I hope you don't mind my recommending some other topics you could consider adding to future editions of your book:
  • A chapter of different asynchronous techniques (HandlerThread and of course Loaders)
  • Mentioning the IntentService class in the chapter of Services
  • And including the use of the Singleton pattern in the SQLite chapter


  • Your book is very good as it is, and I'd still recommend it to anyone who wanted to start in Android, but with a few additions, I'm sure it would become THE perfect choice for every new Android developer.
    Once again, thank you not only for your Android book, but also for all the wonderful books your company produce. They're excellent and they are certainly my first choice when I want to learn a new technology.

     
    Joel Murach
    Author
    Ranch Hand
    Posts: 77
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for your recommendations on topics to add to the next edition. I have made a note of them and will seriously consider each one.

    Thanks also for your kind words about my Android book and about other books that our company publishes. That's what keeps us going!
     
    Karthik Shiraly
    Bartender
    Posts: 1210
    25
    Android C++ Java Linux PHP Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jhon Barrantes, have a cow for your well thought out question and suggestions.
     
    Jhon Barrantes
    Greenhorn
    Posts: 17
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Joel.
    Thank you for taking into account my suggestions. I'm sure you'll knock it out of the park explaining those topics

    Regarding what you mentioned about not always being necessary to use a background thread for SQLite operations, I think you're right.
    There's some apps that are quite simple and don't make an extensive use of a db. For that kind of apps, I think I might consider doing the SQLite operations on the UI thread and see how it goes.

    Finally, do you know if there's any way to get in touch with the Android team? I want to know if it is possible to request some improvements in certain classes? The AsyncQueryHandler class for instance,it has been around since API level 1, without being updated for later additions to the Android platform. Hence, there are some newer functions that require a more general asynchronous handling. For that reason I'd like to see if they could consider updating that class or perhaps coming up with a new one.

    Thanks.
     
    Jhon Barrantes
    Greenhorn
    Posts: 17
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Karthik. I really appreciate your kind words
     
    Karthik Shiraly
    Bartender
    Posts: 1210
    25
    Android C++ Java Linux PHP Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jhon Barrantes wrote:Finally, do you know if there's any way to get in touch with the Android team? I want to know if it is possible to request some improvements in certain classes?

    Check https://source.android.com/source/life-of-a-bug.html on how to file a feature request, or contribute your own patch.
     
    Jhon Barrantes
    Greenhorn
    Posts: 17
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Karthik Shiraly wrote:
    Check https://source.android.com/source/life-of-a-bug.html on how to file a feature request, or contribute your own patch.

    Thank you! I'll definitely file a request for the AsyncQueryHandler.
    By the way, here's the direct link : https://code.google.com/p/android/issues/entry?template=Feature%20request

    Thanks.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic