I am trying to implement Architecture Components from Codelabs. But I'm getting the below error
Do not place Android context classes in static fields (static reference to FarmerNetworkDataSource which has field mContext pointing to Context); this is a memory leak (and also breaks Instant Run) less...
I used the code snippet directly from Google Codelabs. You can refer it here
I have seen some duplicate questions. But i can't figure out which is the best pratice to follow.
Please help & Guide me...
CODE
public class FarmerNetworkDataSource {
// For Singleton instantiation
private static final Object LOCK = new Object();
private static FarmerNetworkDataSource sInstance;
private final Context mContext;
private final AppExecutors mExecutors;
private FarmerNetworkDataSource(Context context, AppExecutors executors) {
mContext = context;
mExecutors = executors;
}
/**
* Get the singleton for this class
*/
public static FarmerNetworkDataSource getInstance(Context context, AppExecutors executors) {
Log.d(LOG_TAG, "Getting the network data source");
if (sInstance == null) {
synchronized (LOCK) {
sInstance = new FarmerNetworkDataSource(context.getApplicationContext(), executors);
Log.d(LOG_TAG, "Made new network data source");
}
}
return sInstance;
}
}
REPOSITORY
public static FarmerRepository provideRepository(Context context) {
AppExecutors executors = AppExecutors.getInstance();
FarmerNetworkDataSource networkDataSource =
FarmerNetworkDataSource.getInstance(context.getApplicationContext(), executors);
return FarmerRepository.getInstance(networkDataSource, executors);
}