Issue
Here is what I'm trying to accomplish. First, I know my application is going to do a lot of I/O. I have read that it's not a good idea to perform this on the main thread. I have seen it recommended that we should use background threads for this. I know we can use Service
s but I think using AsyncTask
would be cleaner and make more sense for my particular application. I have created a package in my project named 'util' where I place a class called FileHelper. This class is going to contain methods to perform most of my I/O operations for the app. This means I will need to pass in a Context
object. I just have a question about implementation. I have started constructing my class like this:
public class FileHelper {
public static Match createMatchFromFile(String fileName) {
File file = new File(fileName);
String jsonString = "";
if (file.exists()) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
jsonString = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return new Gson().fromJson(jsonString, Match.class);
}
public static boolean writeMatchToFile(Context context, String filename, String jsonString){
context = context.getApplicationContext();
FileOutputStream fos = null;
File file = new File(filename);
try {
fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(jsonString.getBytes());
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
assert fos != null;
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static class WriteMatchToFile extends AsyncTask<Match, Void, Void>{
Context mContext;
public WriteMatchToFile(Context context) {
mContext = context.getApplicationContext();
}
@Override
protected Void doInBackground(Match... matches) {
return null;
}
}
}
look at the static class of WriteMatchToFile. I'm using an AsyncTask. I want to call it from the Activity like this:
new FileHelper.WriteMatchToFile(Context context).execute(Match match);
I don't want to place this method in an Activity because I will be calling it throughout the entire application. The problem is 'Lint' is yelling at me and telling me that I'm leaking the context in the WriteMatchToFile class. It's saying that "Non-static inner classes have a implicit reference to their outer classes." But I thought I defined this AsyncTask as statc?!?
Is there a better way to implement this code?
Solution
Its yelling at you because you potentially are- threads holding onto a Context will prevent that Context from being GCed until the thread ends. If your thread is doing something like short lived disk IO (reading/writing a file) that's ok and you can ignore this warning. It will only hold up gcing it for a few hundred ms. If its doing some long term processing, then you should look for a way to only hold the Context as long as possible. Also if its long term, you should use a Thread instead of AsyncTask, as an AsyncTask will hold up other AsyncTasks.
Your usage here appears fine, the write to disk is relatively quick.
Answered By - Gabe Sechan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.