@@ -241,24 +241,54 @@ public static void SaveLocalNotificationData()
241241
242242 public static async Task LoadLocalNotificationData ( )
243243 {
244- await using FileStream fileStream = File . Open ( AppNotifIgnoreFile , FileMode . OpenOrCreate , FileAccess . ReadWrite ) ;
245- if ( ! File . Exists ( AppNotifIgnoreFile ) )
244+ FileStream ? fileStream = null ;
245+
246+ bool forceCreate = false ;
247+ while ( true )
246248 {
247- await new NotificationPush ( ) . SerializeAsync ( fileStream , NotificationPushJsonContext . Default . NotificationPush ) . ConfigureAwait ( false ) ;
248- }
249+ try
250+ {
251+ fileStream = File . Open ( AppNotifIgnoreFile , forceCreate ? FileMode . Create : FileMode . OpenOrCreate , FileAccess . ReadWrite ) ;
252+ if ( fileStream . Length == 0 )
253+ {
254+ await new NotificationPush ( )
255+ . SerializeAsync ( fileStream , NotificationPushJsonContext . Default . NotificationPush )
256+ . ConfigureAwait ( false ) ;
257+ }
249258
250- fileStream . Position = 0 ;
251- NotificationPush ? localNotificationData = await fileStream . DeserializeAsync ( NotificationPushJsonContext . Default . NotificationPush ) . ConfigureAwait ( false ) ;
259+ fileStream . Position = 0 ;
260+ NotificationPush ? localNotificationData = await fileStream
261+ . DeserializeAsync ( NotificationPushJsonContext . Default . NotificationPush )
262+ . ConfigureAwait ( false ) ;
252263
253- if ( NotificationData == null )
254- {
255- return ;
256- }
264+ if ( NotificationData == null )
265+ {
266+ return ;
267+ }
268+
269+ NotificationData . AppPushIgnoreMsgIds = localNotificationData ? . AppPushIgnoreMsgIds ;
270+ NotificationData . RegionPushIgnoreMsgIds = localNotificationData ? . RegionPushIgnoreMsgIds ;
271+ NotificationData . CurrentShowMsgIds = localNotificationData ? . CurrentShowMsgIds ;
272+ NotificationData . EliminatePushList ( ) ;
257273
258- NotificationData . AppPushIgnoreMsgIds = localNotificationData ? . AppPushIgnoreMsgIds ;
259- NotificationData . RegionPushIgnoreMsgIds = localNotificationData ? . RegionPushIgnoreMsgIds ;
260- NotificationData . CurrentShowMsgIds = localNotificationData ? . CurrentShowMsgIds ;
261- NotificationData . EliminatePushList ( ) ;
274+ return ;
275+ }
276+ catch
277+ {
278+ if ( forceCreate )
279+ {
280+ throw ;
281+ }
282+ forceCreate = true ;
283+ }
284+ finally
285+ {
286+ if ( fileStream != null )
287+ {
288+ await fileStream . DisposeAsync ( ) ;
289+ }
290+ }
291+ }
262292 }
263293 }
264294}
0 commit comments