In my Flutter app I fetch about 20,000 items from my Supabase backend and then save it my local storage using Floor. Fetching is fast but looping through is what takes a lot of time and its bad on UX.
@override
Future<void> saveProfiles(List<Profile> profiles) async {
try {
logger('${profiles.length} profiles to be saved');
if (profiles.isNotEmpty) {
for (final profile in profiles) {
await _database.profilesDao.insertProfile(profile);
}
}
} catch (e) {
logger('Unable to save profiles: $e');
}
}
My app is supposed to profile offline access because the target market are in the rural areas otherwise I would not be going through all this pain to do this. So I tried to set up workmanager like this in my bloc
class ProfilesSaveBloc extends Bloc<ProfilesSaveEvent, ProfilesSaveState> {
ProfilesSaveBloc() : super(const _ProfilesSaveState()) {
on<SaveProfiles>(_onSaveProfiles);
}
void _onSaveProfiles(
SaveProfles event,
Emitter<ProfilesSaveState> emit,
) async {
emit(const ProfilesLoadingState());
Workmanager().registerOneOffTask(
'saveProfilesTask',
'saveProfilesTask',
inputData: {
'profiles': event.profiles.map((profile) => profile.toJson()).toList(),
},
);
emit(const ProfilesSavedState());
}
}
Now in my main.dart I did do my stuff there too
@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) async {
try {
await configureDependencies(Environments.production);
switch (task) {
case 'saveProfilesTask':
final List<dynamic> rawData = inputData!['profiles'];
final profiles = rawData.map((json) => Profle.fromJson(json)).toList();
final dbRepo = getIt<DatabaseRepository>();
await dbRepo.saveProfiles(profiles);
break;
}
} catch (e) {
logger('Callback failed: $e');
}
return Future.value(true);
});
}
Future<void> main() async {
logger('Starting app from main.dart');
WidgetsFlutterBinding.ensureInitialized();
await configureDependencies(Environments.production);
Workmanager().initialize(callbackDispatcher, isInDebugMode: false);
runApp(const MyApp());
}
My dependency injection which uses code generation thanks to get_it looks like this
final getIt = GetIt.instance;
@InjectableInit(
initializerName: r'initGetIt',
generateForDir: ['lib'],
)
Future<void> configureDependencies(String environment) async {
logger('Using environment: $environment');
await getIt.initGetIt(environment: environment);
await getIt.allReady();
}
@module
abstract class RegisterModule {
@singleton
@preResolve
Future<SharedPreferences> prefRepo() => SharedPreferences.getInstance();
@singleton
@preResolve
Future<AppDatabase> provideAppDatabase() async => await $FloorAppDatabase
.databaseBuilder(await AppConstants.databaseFile)
.build();
@lazySingleton
DatabaseRepository provideDatabaseRepository(AppDatabase appDatabase) =>
DatabaseRepositoryImpl(appDatabase);
}
dynamic _parseAndDecode(String response) => jsonDecode(response);
dynamic parseJson(String text) =>
compute<String, dynamic>(_parseAndDecode, text);
I am sure I followed all the guidelines including the android manifest
android:enableOnBackInvokedCallback="true"
But my workmanager doesn't fire up and do what I need it to do. I am frustrated
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745107979a4611672.html
评论列表(0条)