Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegate / DataSource || Race Condition ? #518

Closed
aug2uag opened this issue Dec 21, 2016 · 7 comments
Closed

Delegate / DataSource || Race Condition ? #518

aug2uag opened this issue Dec 21, 2016 · 7 comments

Comments

@aug2uag
Copy link

aug2uag commented Dec 21, 2016

I have two scenarios where I programmatically create FSCalendar with a custom FSCalendarCell class.

CustomCell.h:

#import <FSCalendar/FSCalendar.h>

@interface CustomCell : FSCalendarCell

- (void)setCalendarCellWith:(NSDate *)date isSelected:(BOOL)selected;

@end

CustomCell.m

#import "Constants.h"
#import "CustomElements.h"
#import "CustomCell.h"

@interface CustomCell ()

@property (strong, nonatomic) UILabel* l1, *l2;

@end

@implementation MSCalendarCell

- (void)setCalendarCellWith:(NSDate *)date isSelected:(BOOL)selected {
    if (self.day == nil) {
       //  .. init methods
        }
    }
    
    [self.day setAttributedText:  attrStr1];
    [self.date setAttributedText: attrStr2];
    
    if (selected) {
        [self setBackgroundColor: K_COLOR_RED];
    } else {
        [self setBackgroundColor: K_COLOR_YELLOW];
    }
}

@end

With setting delegates after FSCalendar customization options, I don't get a crash, however, I miss delegates (calendar:didSelectDate:, minimumDateForCalendar:) .. I can tell because the program passes through breakpoints here.

ViewController.m

    self.calendar = [[FSCalendar alloc] initWithFrame: calendarRect];
    self.calendar.scope = FSCalendarScopeWeek;
    [self.calendar selectDate: self.selectedDate];
    self.calendar.scopeGesture.enabled = NO;
    self.calendar.appearance.borderRadius = 0;
    self.calendar.appearance.headerTitleColor = K_COLOR_WHITE;
    self.calendar.appearance.caseOptions = FSCalendarCaseOptionsHeaderUsesUpperCase;
    [self.calendar.calendarHeaderView setBackgroundColor: [UIColor lightGrayColor]];
    self.calendar.dataSource = self;
    self.calendar.delegate = self;
    [self.calendar registerClass:[MSCalendarCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.calendar];

Following the example with the configureVisibleCells, the calendar doesn't update because delegates are not called.

However, if I move self.calendar.dataSource = self; and self.calendar.delegate = self; to below the init method, the breakpoints do stop within the delegates. However, I get a nasty crash:

2016-12-20 16:06:57.956 Mobile Styles[9198:42231530] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FSCalendarCell setCalendarCellWith:isSelected:]: unrecognized selector sent to instance 0x7ff4ff41c410'
*** First throw call stack:
(
	0   CoreFoundation                      0x00000001094d534b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x000000010d4db21e objc_exception_throw + 48
	2   CoreFoundation                      0x0000000109544f34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
	3   CoreFoundation                      0x000000010945ac15 ___forwarding___ + 1013
	4   CoreFoundation                      0x000000010945a798 _CF_forwarding_prep_0 + 120
	5   XXX .                                      0x00000001081411fc -[ScheduleServiceViewController calendar:willDisplayCell:forDate:atMonthPosition:] + 396
	6   FSCalendar                          0x000000010a63d081 -[FSCalendarDelegateProxy willDisplayCell:forDate:atMonthPosition:] + 353
	7   FSCalendar                          0x000000010a610df4 -[FSCalendar collectionView:willDisplayCell:forItemAtIndexPath:] + 324
	8   UIKit                               0x000000010c091116 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 2409
	9   UIKit                               0x000000010c0907a7 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 35
	10  UIKit                               0x000000010c095c7f -[UICollectionView _updateVisibleCellsNow:] + 4803
	11  UIKit                               0x000000010c09b913 -[UICollectionView layoutSubviews] + 313
	12  UIKit                               0x000000010b812f50 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
	13  QuartzCore                          0x000000010b178cc4 -[CALayer layoutSublayers] + 146
	14  QuartzCore                          0x000000010b16c788 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
	15  UIKit                               0x000000010b800ff0 -[UIView(Hierarchy) layoutBelowIfNeeded] + 1507
	16  FSCalendar                          0x000000010a6202d5 -[FSCalendarAnimator performTransitionCompletion:animated:] + 1349
	17  FSCalendar                          0x000000010a61fd84 -[FSCalendarAnimator performTransitionCompletionAnimated:] + 84
	18  FSCalendar                          0x000000010a61ea6e -[FSCalendarAnimator performScopeTransitionFromScope:toScope:animated:] + 1822
	19  FSCalendar                          0x000000010a60f091 -[FSCalendar layoutSubviews] + 5969
	20  UIKit                               0x000000010b812f50 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
	21  QuartzCore                          0x000000010b178cc4 -[CALayer layoutSublayers] + 146
	22  QuartzCore                          0x000000010b16c788 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
	23  QuartzCore                          0x000000010b16c606 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
	24  QuartzCore                          0x000000010b0fa680 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
	25  QuartzCore                          0x000000010b127767 _ZN2CA11Transaction6commitEv + 475
	26  QuartzCore                          0x000000010b1280d7 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
	27  CoreFoundation                      0x0000000109479e17 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
	28  CoreFoundation                      0x0000000109479d87 __CFRunLoopDoObservers + 391
	29  CoreFoundation                      0x000000010945e4b6 CFRunLoopRunSpecific + 454
	30  UIKit                               0x000000010b7487e6 -[UIApplication _run] + 434
	31  UIKit                               0x000000010b74e964 UIApplicationMain + 159
	32  Mobile Styles                       0x000000010817b65f main + 111
	33  libdyld.dylib                       0x000000010db1068d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
@aug2uag
Copy link
Author

aug2uag commented Dec 21, 2016

I used FSCalendar CocoaPods, using XCode 8.1, running iOS 10.1

@aug2uag
Copy link
Author

aug2uag commented Dec 21, 2016

Just noticed:

    self.calendar = [[FSCalendar alloc] initWithFrame: calendarRect];
    self.calendar.dataSource = self;
    self.calendar.delegate = self;
    self.calendar.scope = FSCalendarScopeWeek;
    [self.calendar selectDate: self.selectedDate];
    ...

and

    self.calendar = [[FSCalendar alloc] initWithFrame: calendarRect];
    self.calendar.delegate = self;
    self.calendar.dataSource = self;
    self.calendar.scope = FSCalendarScopeWeek;
    [self.calendar selectDate: self.selectedDate];
    ...

and

    self.calendar = [[FSCalendar alloc] initWithFrame: calendarRect];
    self.calendar.delegate = self;
    self.calendar.scope = FSCalendarScopeWeek;
    [self.calendar selectDate: self.selectedDate];
    ...
    self.calendar.dataSource = self;

cause crash

however, with the configuration below there's no crash, yet, delegates do not work

    self.calendar = [[FSCalendar alloc] initWithFrame: calendarRect];
    self.calendar.dataSource = self;
    self.calendar.scope = FSCalendarScopeWeek;
    [self.calendar selectDate: self.selectedDate];
    ...
    self.calendar.delegate = self;

@aug2uag aug2uag changed the title Setting cell height || Trouble with delegates Delegate / DataSource || Race Condition ? Dec 21, 2016
@aug2uag
Copy link
Author

aug2uag commented Dec 21, 2016

This problem doesn't exist when I import FSCalendar in to my project without Cocoapods, however, now there is another exception that I'll be working to correct:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSMethodSignature signatureWithObjCTypes:]: type signature is NULL.'
*** First throw call stack:
(
	0   CoreFoundation                      0x0000000103e8e34b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x0000000107e0321e objc_exception_throw + 48
	2   CoreFoundation                      0x0000000103e10130 +[NSMethodSignature signatureWithObjCTypes:] + 1824
	3   XXX .                                   0x0000000102b0ba3a -[FSCalendarDelegationProxy methodSignatureForSelector:] + 490
	4   CoreFoundation                      0x0000000103e13968 ___forwarding___ + 328
	5   CoreFoundation                      0x0000000103e13798 _CF_forwarding_prep_0 + 120
	6   Foundation                          0x000000010793043e -[NSProxy respondsToSelector:] + 64
	7   XXX .                                   0x0000000102b0b634 -[FSCalendarDelegationProxy respondsToSelector:] + 244
	8   XXX .                                    0x0000000102ae2fc6 -[FSCalendar invalidateAppearanceForCell:forDate:] + 982
	9   XXX .                                   0x0000000102ae469f -[FSCalendar reloadDataForCell:atIndexPath:] + 2303
	10  XXX .                                   0x0000000102ad91cc -[FSCalendar collectionView:cellForItemAtIndexPath:] + 444
	11  UIKit                               0x00000001069b8980 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 467
	12  UIKit                               0x00000001069b87a7 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 35
	13  UIKit                               0x00000001069bdc7f -[UICollectionView _updateVisibleCellsNow:] + 4803
	14  UIKit                               0x00000001069c3913 -[UICollectionView layoutSubviews] + 313
	15  UIKit                               0x000000010613af50 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
	16  QuartzCore                          0x0000000105aa0cc4 -[CALayer layoutSublayers] + 146
	17  QuartzCore                          0x0000000105a94788 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
	18  QuartzCore                          0x0000000105a94606 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
	19  QuartzCore                          0x0000000105a22680 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
	20  QuartzCore                          0x0000000105a4f767 _ZN2CA11Transaction6commitEv + 475
	21  QuartzCore                          0x0000000105a500d7 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
	22  CoreFoundation                      0x0000000103e32e17 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
	23  CoreFoundation                      0x0000000103e32d87 __CFRunLoopDoObservers + 391
	24  CoreFoundation                      0x0000000103e174b6 CFRunLoopRunSpecific + 454
	25  GraphicsServices                    0x000000010afbea6f GSEventRunModal + 161
	26  UIKit                               0x0000000106076964 UIApplicationMain + 159
	27  Mobile Styles                       0x0000000102ae738f main + 111
	28  libdyld.dylib                       0x000000010843868d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

@aug2uag aug2uag closed this as completed Dec 21, 2016
@WenchaoD
Copy link
Owner

Hi @aug2uag Does this commit help with the last issue?

@aug2uag
Copy link
Author

aug2uag commented Dec 21, 2016

@WenchaoD delegates work, and program no longer crashed with +[NSMethodSignature signatureWithObjCTypes:]: type signature is NULL.

I can tell delegate works because of the behavior with minimumDateForCalendar: and maximumDateForCalendar:.

However, I cannot find the callback when I scroll weeks .. calendar:didSelectDate:, calendar:didDeselectDate:atMonthPosition, calendar:cellForDate:atMonthPosition did not stop at breakpoint for me to call my configureVisibleCells method.

The data is lost at the scrolled cells since there's no way to update their values, as here in this link.

By the way, if you can also show me how to resize cells .. I'm trying to get 40px height per cell and having no luck with setFrame:

@aug2uag
Copy link
Author

aug2uag commented Dec 22, 2016

I had to add a delegate to FSCalendar at reloadVisibleCells and to access the rowHeight attribute at layoutSubviews in order to get the behavior I wanted. Now I get separator lines in between every two cells that I need to remove, in case you can assist would be great.

@WenchaoD
Copy link
Owner

The last problem is fixed in development branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants