JTAppleCalendar icon indicating copy to clipboard operation
JTAppleCalendar copied to clipboard

Selecting a large date range all at once shows a delay before loading app [Expected]. Suggestions wanted on how to improve this code.

Open gymbay opened this issue 6 years ago • 12 comments

Version Number: 8.0.2

Description

Function "selectDates(from startDate: Date, to endDate: Date, triggerSelectionDelegate: Bool = true, keepSelectionIfMultiSelectionAllowed: Bool = false)" freeze main thread in my app when I pass too long period between startDate and endDate (about six and more months).

Steps To Reproduce

func calendar(_ calendar: JTACMonthView, didSelectDate date: Date, cell: JTACDayCell?, cellState: CellState, indexPath: IndexPath) {
               var selectedDates = calendar.selectedDates
               if let start = selectedDates.min(), let end = selectedDates.max() {
                              calendar.selectDates(from: start, to: end, triggerSelectionDelegate: false, keepSelectionIfMultiSelectionAllowed: true)
               }
}

Expected Behavior

Not freeze main thread in app for a few seconds

gymbay avatar Dec 27 '19 12:12 gymbay

you are calling selectDates inside of didSelect. infinite loop?

patchthecode avatar Dec 27 '19 20:12 patchthecode

No infinite loop here cause triggerSelectionDelegate = false. We are based on your sample code "2 Tap Ranged Selection" from Wiki. And that code also frizzed. Screen record in attachment.

screen-record.zip

gymbay avatar Dec 30 '19 07:12 gymbay

Ok, so to clarify, You issue is --> When you select a Large range of dates, you application freezes until the selection is complete?

patchthecode avatar Dec 30 '19 11:12 patchthecode

Yes that's it

gymbay avatar Dec 30 '19 13:12 gymbay

can you try the menthod mentioned here? https://github.com/patchthecode/JTAppleCalendar/issues/1150

calendar.selectDates(dateRange, triggerSelectionDelegate: false, keepSelectionIfMultiSelectionAllowed: true)

patchthecode avatar Dec 30 '19 18:12 patchthecode

Not worked for me. And I can see that method also invoke in selectDates(from:to:). According to my tests, the following code in the method selectDates(_ dates: triggerSelectionDelegate:keepSelectionIfMultiSelectionAllowed:) slows down: Снимок экрана 2019-12-31 в 15 40 08

gymbay avatar Dec 31 '19 12:12 gymbay

@patchthecode please help!

CibGit777 avatar Mar 13 '20 08:03 CibGit777

i think you may have to help me with a solution here.

Imagine i am selection 1,000,000 dates. Do you guys think that this should take some time to select? To me, the answer would be yes.

Now, can you suggest a solution that i can implement that will make this not happen? If you can provide me a suggestion, then I can provide a fix. Right now, i cannot think of a way to select so many dates, with zero impact to performance.

patchthecode avatar Mar 13 '20 12:03 patchthecode

Ran into the same problem, but I understand that selecting a large range of dates would cause performance issues. Might I suggest implementing a completion handler so that if the selection is taking some time, developers can present a progress indication to the user. Would work great for me 😌

blastback360 avatar Apr 16 '20 03:04 blastback360

@blastback360 sounds like a good idea. I flagged this with the help wanted.

If anyone is willing to take this up i'd be happy. Peronally though, i'd want to know a reason for why we'd select so many dates up front all at once. I am wondering if there is something else the dev can do given the limitations of this lib.

patchthecode avatar Apr 22 '20 12:04 patchthecode

Hey guys, for huge amount of dates seems that the best way is to avoid any delegates related to selection etc and use own logic based on two dates - for example in "handleCellSelected" check if date is laying between that range and if yes - highlight view on the fly. I don't think that we need to do hard calculation for every IndexPath if we can do it only for visible cells. In this case even selection of 1 million will be efficient

pavserg avatar May 19 '20 11:05 pavserg

give this man a medal. I think i can close this issue. The dev should be the one to implement something to help this, not the library.

patchthecode avatar May 19 '20 12:05 patchthecode