CareKit icon indicating copy to clipboard operation
CareKit copied to clipboard

Updated Content

Open majidsaleem105 opened this issue 1 year ago • 16 comments

Hi @gavirawson-apple,

We're using the CareKit 2.0 version and facing a strange issue. If we open the app, use it for a while to complete some of the tasks, and leave it open in the background. Now, suppose we do not open the app until midnight, and the date changes. If we open the app the next morning, it shows the events from the previous date instead of today's event. It shows the completed event from yesterday instead of incomplete events for today.

We have implemented the pull-down refresh feature to sync the tasks with the server and reload the screen, but it doesn't help to bring the events of the current day. We have to swipe out of the app and open it again to see today's events/tasks.

Can you please advise us on this? How can we resolve this problem?

Thank You,

  • Majid S.

majidsaleem105 avatar Jun 20 '23 14:06 majidsaleem105

Hey @majidsaleem105, I'll ask a few questions to get a better sense of the setup in your app -

  • How and when are you generating the event query?
  • Are you using a pre-packaged CareKit view controller or custom code?
  • Can you share the code you're using to fetch and display the tasks?

gavirawson-apple avatar Jun 21 '23 00:06 gavirawson-apple

Hey @gavirawson-apple,

Q: How and when are you generating the event query? A: The function override func dailyPageViewController is used to build the home screen. Here is the query that fetches data:

var query = OCKTaskQuery(for: date) query.excludesTasksWithNoEvents = true storeManager.store.fetchAnyTasks(query: query, callbackQueue: .main) { [self] result in // Custom logic }

Q: Are you using a pre-packaged CareKit view controller or custom code? A: We are using pre-packed with custom code.

Q: Can you share the code you're using to fetch and display the tasks? A: You can find the code here. https://gist.github.com/majidsaleem105/588ee96e0fa373ad49ddcb38ac63c302

Please let me know if you any additional information.

Thanks,

  • Majid S.

majidsaleem105 avatar Jun 27 '23 10:06 majidsaleem105

Hey @gavirawson-apple,

Any suggestions? Would you happen to need any more information?

Thanks,

  • Majid S.

majidsaleem105 avatar Jul 10 '23 11:07 majidsaleem105

Hey @gavirawson-apple,

Please guide us on how we can resolve this issue.

Thanks,

  • Majid S.

majidsaleem105 avatar Jul 17 '23 10:07 majidsaleem105

@majidsaleem105 thanks for the additional info. It looks to me like the pull to refresh feature implemented in the OCKDailyPageViewController will reload the tasks for the currently selected date. I don't think it will select a new date once midnight passes, and then load the tasks for that new date. Are you expecting that behavior?

You can likely confirm if that's the underlying issue by looking at the calendar on the top of the OCKDailyPageViewController. After midnight passes, is the old day still selected?

gavirawson-apple avatar Jul 18 '23 03:07 gavirawson-apple

Thanks, @gavirawson-apple, for your reply.

That's correct. We want to use the new date after the midnight passes.

Also, the calendar shows the old day selected after midnight if we don't swipe out of the app and bring it from the opened apps from the background.

I'll go ahead and confirm the selected day again and let you know.

Thank You.

  • Majid S.

majidsaleem105 avatar Jul 18 '23 10:07 majidsaleem105

As long as the correct tasks for the selected day are shown, there might not be a bug lurking here. If the displayed tasks do not correspond to the selected day, there's certainly a bug.

If you'd like to refresh the selected date after midnight passes, I'd recommend hooking into this notification. A notification will get published around when midnight passes, and you can then go and refresh the selected date to match the current date.

gavirawson-apple avatar Jul 18 '23 16:07 gavirawson-apple

Hey @gavirawson-apple,

I've verified that the top calendar shows yesterday as selected after midnight passes. Also, I used the significantTimeChangeNotification notification to get the updated date, but it didn't help. I might have used it inaccurately.

I used the following line of code in the viewDidLoad function in the CareViewController: NotificationCenter.default.addObserver(self, selector: #selector(dayChanged), name: UIApplication.significantTimeChangeNotification, object: nil)

then @objc func dayChanged(_ notification: Notification) to reload the table content.

This way, it simply refreshes the screen with the old date instead of the new date.

Can you guide me if I'm using it inaccurately?

Kind Regards,

  • Majid S.

majidsaleem105 avatar Jul 24 '23 10:07 majidsaleem105

What does you implementation of dayChanged(notification:) look like? I imagine inside of it you're going to need to call OCKDailyPageViewController.selectDate(date:animated:)

gavirawson-apple avatar Jul 25 '23 05:07 gavirawson-apple

@gavirawson-apple

After making your suggested changes, dayChanged function will look like this:

@objc func dayChanged(_ notification: Notification) { OCKDailyPageViewController(storeManager: self.storeManager).selectDate(Date(), animated: false) }

Am I using it in the correct way?

majidsaleem105 avatar Jul 25 '23 14:07 majidsaleem105

Looks right to me! Does that fix your issue?

gavirawson-apple avatar Jul 28 '23 21:07 gavirawson-apple

Thanks, @gavirawson-apple but it didn't fix my problem. It still shows content from the last day.

majidsaleem105 avatar Aug 01 '23 06:08 majidsaleem105

I've tried this out in the CareKitSample app and it seems to be working. Try cloning that project and opening CareViewController. Add this new code to refresh the view controller when midnight passes:

override func viewDidLoad() {
    super.viewDidLoad()

    let changeNotificationName = UIApplication.significantTimeChangeNotification
    NotificationCenter.default.addObserver(self, selector: #selector(selectCurrentDate), name: changeNotificationName, object: nil)

    // ...
}

@objc private func selectCurrentDate() {
    selectDate(Date(), animated: true)
}

I'm not exactly sure why it's not working in your app. Is the code set up in a similar way as the snippet above? Are there any major differences that may be interfering here?

gavirawson-apple avatar Aug 30 '23 16:08 gavirawson-apple

Hey @majidsaleem105, did this solve your issue or are you still hitting a problem?

gavirawson-apple avatar Nov 10 '23 18:11 gavirawson-apple

Hey @gavirawson-apple,

I am sorry for the delayed reply. I was super busy with other stuff and couldn't spare time to validate this yet. I'll try this and keep you posted.

Thanks a lot for your time and for sharing this sample. Kind Regards,

  • Majid S.

majidsaleem105 avatar Nov 17 '23 07:11 majidsaleem105

No problem at all! Take your time, just wanted to check in. We're here if you need us.

gavirawson-apple avatar Nov 17 '23 22:11 gavirawson-apple