3

Can anyone help why the old score is showing in live activity after 8 hours?

I have added the live activity on Monday, 24 April 9:50 PM. It's working fine to update data properly via notification. But, I see old data in next morning. Old score matches on added time data.

I don't know why happening this. I have attached 3 screenshots for better understanding. So, if anyone has an idea about this please ping me. enter image description here

Code For add Live activity

var contentState = LiveScoreAttributes.Status(value: "This is dynamic app!", id: liveScoreViewModelObj.allLiveScores?[self.pagecontrol.currentPage].id ?? 0, liveScore: LiveScoreModel(allLiveScores: [myData]))
do {
    activityScoreAttributes = try Activity<LiveScoreAttributes>.request(attributes: attributes, contentState: contentState, pushType: .token)
}
catch (let error) {
    print(error.localizedDescription)
}

Code For Update Live activity

let contentState = LiveScoreAttributes.Status(value: "This is dynamic app!", id: myId, liveScore: LiveScoreModel(allLiveScores: [myData]))
Task {
    for activity in Activity<LiveScoreAttributes>.activities {
        if liveScore.id == activity.content.state.id {
              await activity.update(using: contentState)
        }
    }
}
Chandresh Kachariya
  • 667
  • 2
  • 13
  • 31
  • You haven’t shown much that is relevant but from the snippet you provided the update code is likely dead immediately since you aren’t holding on to the task. – lorem ipsum Apr 26 '23 at 11:56
  • No, it is working fine. I am having issue every morning. showing old data in live activity @loremipsum – Chandresh Kachariya Apr 26 '23 at 12:13
  • Like I said you haven’t provided anything relevant to help you. Just 2 tiny code snippets. Where do the updates come from? What background modes are you using? What troubleshooting have you done and what have you gotten from the troubleshooting? If you haven’t done any of that start your Google search with “how to update a live activity” maybe even add “background” to that. Apple has extensive documentation on the subject. – lorem ipsum Apr 26 '23 at 12:17
  • Everything I checked. Basically, I sending the silent push notification from AWS and I receiving it to update data properly. See the middle screenshot. But, When I see in the morning I see old data. I shared code in the post on how I update data – Chandresh Kachariya Apr 26 '23 at 12:27
  • I have no issues to update data. I have issues in next morning(After 8 hours) – Chandresh Kachariya Apr 26 '23 at 12:28
  • Have you made sure that something isn’t updating the activity with old data? Do you have logs in place? – lorem ipsum Apr 26 '23 at 12:38
  • If the game is over why is the live activity still running after 8hrs? If I remember correctly there is something in the Apple documentation of 4hrs. I don’t remember the exact wording but there is something there. – lorem ipsum Apr 26 '23 at 12:42
  • After finish game, I stop to sending notification from AWS. Also, I checked all log, and not get any mistake – Chandresh Kachariya Apr 26 '23 at 12:45
  • You need to stop the live activity too. – lorem ipsum Apr 26 '23 at 12:48
  • No, We need to show live activity as per the requirement – Chandresh Kachariya Apr 26 '23 at 12:53
  • Apple says that you have to end the activity to ensure the final content is what stays displayed. There is no way around that. You get to chose it stays up for up to 4 hrs. Apple and the user can decide to remove it whenever they want. – lorem ipsum Apr 26 '23 at 12:54

1 Answers1

0

You have to “end the activity to ensure the live activity shows the latest and final update”

Activity, include a final content update using the content parameter to ensure the Live Activity shows the latest and final content update after it ends.

https://developer.apple.com/documentation/activitykit/activity/end(_:dismissalpolicy:)

Always end a Live Activity after the associated task or live event ends. A Live Activity that ended remains on the Lock Screen until the person removes it or the system removes it automatically. The automatic removal depends on the dismissal policy you provide to the end(_:dismissalPolicy:) function. Always include an updated Activity.ContentState to ensure the Live Activity shows the latest and final content update after it ends. This is important because the Live Activity can remain visible on the Lock Screen for some time after it ends.

To immediately remove the Live Activity that ended from the Lock Screen, use immediate. Alternatively, use after(_:) to specify a date within a four-hour window. While you can provide any date, the system removes the ended Live Activity after the given date or after four hours from the moment the Live Activity ended — whichever comes first.

https://developer.apple.com/documentation/activitykit/displaying-live-data-with-live-activities

lorem ipsum
  • 21,175
  • 5
  • 24
  • 48
  • Okay, System will remove then it's Okay. But why is show old data? System will remove after 12 hours but after 8 hours why I see an old score? – Chandresh Kachariya Apr 26 '23 at 13:03
  • @ChandreshKachariya it sounds like that it happens by design. Apple is specifically saying you have to **“end the activity to ensure it says updated”.** Apple likely starts showing old data to make sure developers aren’t keeping live activities alive but that is just an assumption. – lorem ipsum Apr 26 '23 at 13:05
  • Okay, Can you please share where is apple say “end the activity to ensure it says updated” – Chandresh Kachariya Apr 26 '23 at 13:09
  • @ChandreshKachariya the 2 links in the answer I have quoted/highlighted the paragraphs exactly. – lorem ipsum Apr 26 '23 at 13:10
  • @ChandreshKachariya if I answered you question can you please click on the green check mark and maybe upvote? – lorem ipsum May 01 '23 at 23:48
  • I am working on that, If I get success I will do it – Chandresh Kachariya May 03 '23 at 04:56