Closed suhailbilalw closed 5 months ago
`StreamController
subscription ??= Alarm.ringStream.stream.listen((event) async { if (await Alarm.isRinging(event.id)) { if (callbackController.hasListener) callbackController.sink.add(event); } });`
Hi @suhailbilalw
1) How often does the crash happen in your Crashlytics ?
2) Do you know if the crash happens at the moment the alarm is set or at the moment the alarm rings ?
3) Do you think it's related to your callbackController
?
Hi @gdelataillade
Hi @suhailbilalw
To fix your issue make sure you use the await
keyword when you call Alarm.set
. I think the issue happens when there are multiple alarms set at the same time. What do you think ?
hello @gdelataillade
void scheduleAlarms() { unawaited(() async { var now = DateTime.now(); for (var alarm in FFAppState().reminders) { if (alarm.repeat == Repeat.once && alarm.dateTime!.isAfter(now)) { await Alarm.set( alarmSettings: AlarmSettings( id: alarm.id, dateTime: alarm.dateTime!, assetAudioPath: 'assets/audios/prayer_end.mp3', loopAudio: true, vibrate: true, volume: 1, fadeDuration: 3.0, notificationTitle: 'Pray Now Reminder', notificationBody: alarm.title, enableNotificationOnKill: true, ), ); } else if (alarm.repeat == Repeat.daily) { for (var i = 0; i < 7; i++) { var dateTime = DateTime.now() .copyWith( hour: alarm.dateTime!.hour, minute: alarm.dateTime!.minute, second: alarm.dateTime!.second, microsecond: 0, millisecond: 0, ) .add(Duration(days: i)); if (dateTime.isAfter(DateTime.now())) { await Alarm.set( alarmSettings: AlarmSettings( id: alarm.id + i, dateTime: dateTime, assetAudioPath: 'assets/audios/prayer_end.mp3', loopAudio: true, vibrate: true, volume: 1, fadeDuration: 3.0, notificationTitle: 'Pray Now Reminder', notificationBody: alarm.title, enableNotificationOnKill: true, ), ); } } } } }()); }
this is my code for setting the alarm. Please let me know if you see any problem with it.
@suhailbilalw I don't see any problem with your code. Have you carefully followed the iOS setup steps in the README ?
Hello @gdelataillade Yeah, I'm sure about it. Because sometimes it's running perfectly fine. Crash is not happening everytime.
@suhailbilalw
Why did you use the unawaited
keyword ? It might be the cause of the crash. Here's an updated version of your code:
Future<void> scheduleAlarms() async {
var now = DateTime.now();
for (var alarm in FFAppState().reminders) {
// rest of your code here...
}
What do you think ?
@gdelataillade I'll try if this solves the problem, will get back to you.
@gdelataillade Can this be a cause to the crash?
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
super.didChangeAppLifecycleState(state);
// _appLifecycleState = state;
switch (state) {
case AppLifecycleState.resumed:
final alarms = Alarm.getAlarms();
for (var alarm in alarms) {
if (alarm.id > 0) await Alarm.stop(alarm.id);
}
break;
case AppLifecycleState.paused:
if (!FFAppState().userIsPraying) {
await scheduleAlarms();
}
break;
case AppLifecycleState.detached:
case AppLifecycleState.inactive:
case AppLifecycleState.hidden:
}
}
I don't think so.
@gdelataillade I got same error today even after removing unawaited function. I don't think it is the root cause.
Hello @gdelataillade I removed stream subscription as well. but error still persists. it is on stopSilentSound, setAlarm and stopAlarm functions
Hi @suhailbilalw
Thanks for the details. I'll try to find out what is causing the issue. Maybe I'm wrong but it looks like you are the only one with the issue. Maybe you could keep trying to edit your code to see what part is causing the issue.
Thank you and sorry for the inconvenience.
Hi @suhailbilalw
To fix your issue make sure you use the
await
keyword when you callAlarm.set
. I think the issue happens when there are multiple alarms set at the same time. What do you think ?
This definetly happened to me and that should be noted somewhere in the plugin general issues, i fixed it already but its happening again now with the alarm.stop even thou i am awaiting them
@gdelataillade Can this be a cause to the crash?
@override void didChangeAppLifecycleState(AppLifecycleState state) async { super.didChangeAppLifecycleState(state); // _appLifecycleState = state; switch (state) { case AppLifecycleState.resumed: final alarms = Alarm.getAlarms(); for (var alarm in alarms) { if (alarm.id > 0) await Alarm.stop(alarm.id); } break; case AppLifecycleState.paused: if (!FFAppState().userIsPraying) { await scheduleAlarms(); } break; case AppLifecycleState.detached: case AppLifecycleState.inactive: case AppLifecycleState.hidden: } }
if you lyfecycle method gets called twice, ie, user foreground twice the app fast and the first alarm stop hasnt finished trying to stop it again could be an issue
@gdelataillade Can this be a cause to the crash?
@override void didChangeAppLifecycleState(AppLifecycleState state) async { super.didChangeAppLifecycleState(state); // _appLifecycleState = state; switch (state) { case AppLifecycleState.resumed: final alarms = Alarm.getAlarms(); for (var alarm in alarms) { if (alarm.id > 0) await Alarm.stop(alarm.id); } break; case AppLifecycleState.paused: if (!FFAppState().userIsPraying) { await scheduleAlarms(); } break; case AppLifecycleState.detached: case AppLifecycleState.inactive: case AppLifecycleState.hidden: } }
if you lyfecycle method gets called twice, ie, user foreground twice the app fast and the first alarm stop hasnt finished trying to stop it again could be an issue
Hi @jhonatan-3a ! this issue is emerging from three different functions, check this https://github.com/gdelataillade/alarm/issues/194#issuecomment-2095141323 so I'm sure my code is fine.
also this package documentation says ===> To avoid unexpected behaviours, if you set an alarm for the same time, down to the second, as an existing one, the new alarm will replace the existing one.
Crashed: com.gdelataillade.alarm.resourceAccessQueue EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000000