@@ -262,7 +262,7 @@ private void Update() {
262
262
263
263
dbref = FirebaseDatabase . DefaultInstance . RootReference ;
264
264
initialized = true ;
265
- RefreshScores ( ) ;
265
+ refreshScores = true ;
266
266
readyToInitialize = false ;
267
267
if ( FirebaseInitialized != null ) {
268
268
FirebaseInitialized ( this , null ) ;
@@ -427,6 +427,7 @@ public Task<UserScore> AddScore(UserScore score) {
427
427
return newEntry . SetValueAsync ( scoreDict ) . ContinueWith ( task => {
428
428
if ( task . Exception != null ) {
429
429
Debug . LogWarning ( "Exception adding score: " + task . Exception ) ;
430
+ return null ;
430
431
}
431
432
if ( ! task . IsCompleted ) {
432
433
return null ;
@@ -508,7 +509,10 @@ private void OnScoreAdded(object sender, ChildChangedEventArgs args) {
508
509
return ;
509
510
}
510
511
511
- var score = new UserScore ( args . Snapshot ) ;
512
+ var score = UserScore . CreateScoreFromRecord ( args . Snapshot ) ;
513
+ if ( score == null ) {
514
+ return ;
515
+ }
512
516
513
517
// Verify that score is within start/end times, and isn't already in TopScores.
514
518
if ( TopScores . Contains ( score ) ) {
@@ -577,7 +581,10 @@ private void OnScoreRemoved(object sender, ChildChangedEventArgs args) {
577
581
if ( args . Snapshot == null || ! args . Snapshot . Exists ) {
578
582
return ;
579
583
}
580
- var score = new UserScore ( args . Snapshot ) ;
584
+ var score = UserScore . CreateScoreFromRecord ( args . Snapshot ) ;
585
+ if ( score == null ) {
586
+ return ;
587
+ }
581
588
if ( TopScores . Contains ( score ) ) {
582
589
TopScores . Remove ( score ) ;
583
590
RefreshScores ( ) ;
@@ -609,7 +616,7 @@ private void GetInitialTopScores(long batchEnd) {
609
616
return ;
610
617
}
611
618
612
- if ( task . Result . ChildrenCount == 0 ) {
619
+ if ( ! task . Result . HasChildren ) {
613
620
// No scores left to retrieve.
614
621
SetTopScores ( ) ;
615
622
return ;
@@ -630,19 +637,20 @@ private void GetInitialTopScores(long batchEnd) {
630
637
631
638
// Until we have found ScoresToRetrieve unique user scores or run out of
632
639
// scores to retrieve, get another page of score records by ending the next batch
633
- // (ordered by score) at the lowest score found so far.
634
- var lastScore = task . Result . Children . First ( ) . Child ( "score" ) . GetRawJsonValue ( ) ;
635
- long score , nextEndAt ;
640
+ // (ordered by score) at the worst score found so far.
641
+ long nextEndAt ;
636
642
if ( LowestFirst ) {
637
- nextEndAt = Int64 . TryParse ( lastScore , out score ) ?
638
- score + 1 :
639
- Math . Min ( Int64 . MaxValue , batchEnd + 1 ) ;
643
+ nextEndAt = scores . First ( ) . Score + 1L ;
640
644
} else {
641
- nextEndAt = Int64 . TryParse ( lastScore , out score ) ?
642
- score - 1 :
643
- Math . Max ( Int64 . MinValue , batchEnd - 1 ) ;
645
+ nextEndAt = scores . Last ( ) . Score - 1L ;
646
+ }
647
+ try {
648
+ GetInitialTopScores ( nextEndAt ) ;
649
+ } catch ( Exception ex ) {
650
+ Debug . LogException ( ex ) ;
651
+ } finally {
652
+ SetTopScores ( ) ;
644
653
}
645
- GetInitialTopScores ( nextEndAt ) ;
646
654
} ) ;
647
655
}
648
656
@@ -653,15 +661,16 @@ private void GetInitialTopScores(long batchEnd) {
653
661
/// <param name="snapshot">DataSnapshot record of user scores.</param>
654
662
/// <param name="startTS">Earliest valid timestamp of a user score to retrieve.</param>
655
663
/// <param name="endTS">Latest valid timestamp of a user score to retrieve.</param>
656
- /// <returns>IEnumerable of valid UserScore objects.</returns>
657
- private IEnumerable < UserScore > ParseValidUserScoreRecords (
664
+ /// <returns>List of valid UserScore objects.</returns>
665
+ private List < UserScore > ParseValidUserScoreRecords (
658
666
DataSnapshot snapshot ,
659
667
long startTS ,
660
668
long endTS ) {
661
669
return snapshot . Children
662
- . Select ( scoreRecord => new UserScore ( scoreRecord ) )
663
- . Where ( score => score . Timestamp > startTS && score . Timestamp <= endTS )
664
- . Reverse ( ) ;
670
+ . Select ( scoreRecord => UserScore . CreateScoreFromRecord ( scoreRecord ) )
671
+ . Where ( score => score != null && score . Timestamp > startTS && score . Timestamp <= endTS )
672
+ . Reverse ( )
673
+ . ToList ( ) ;
665
674
}
666
675
667
676
/// <summary>
0 commit comments