Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Make delegate method optional (and add type specification to delegate…
Browse files Browse the repository at this point in the history
… property)
  • Loading branch information
julianrex committed Mar 27, 2020
1 parent e2b24c0 commit 9d2efab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
19 changes: 9 additions & 10 deletions platform/darwin/src/MGLNetworkConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@ NS_ASSUME_NONNULL_BEGIN

@class MGLNetworkConfiguration;

@protocol MGLNetworkConfigurationDelegate <NSObject>
@optional

/**
:nodoc:
The `MGLNetworkConfigurationSessionDelegate` protocol provides an `NSURLSession`
object for the `MGLNetworkConfiguration`. This API should be considered
experimental, likely to be removed or changed in future releases.
Provides an `NSURLSession` object for the specified `MGLNetworkConfiguration`.
This API should be considered experimental, likely to be removed or changed in
future releases.
This method is called from background threads, i.e. it is not called on the main
thread.
@note Background sessions (i.e. created with
`-[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:]`)
and sessions created with a delegate that conforms to `NSURLSessionDataDelegate`
are not supported at this time.
@note: Sessions created with a delegate that conforms to `NSURLSessionDataDelegate`
are not supported at this time.
*/
@protocol MGLNetworkConfigurationSessionDelegate <NSObject>
- (NSURLSession *)sessionForNetworkConfiguration:(MGLNetworkConfiguration *)configuration;
@end

Expand All @@ -39,7 +38,7 @@ MGL_EXPORT
:nodoc:
Delegate for the `MGLNetworkConfiguration` class.
*/
@property (nonatomic, weak) id delegate;
@property (nonatomic, weak) id<MGLNetworkConfigurationDelegate> delegate;

/**
Returns the shared instance of the `MGLNetworkConfiguration` class.
Expand All @@ -56,7 +55,7 @@ MGL_EXPORT
Assign this object before instantiating any `MGLMapView` object, or using
`MGLOfflineStorage`
@note: `NSURLSession` objects store a copy of this configuration. Any further changes
@note `NSURLSession` objects store a copy of this configuration. Any further changes
to mutable properties on this configuration object passed to a session’s initializer
will not affect the behavior of that session.
Expand Down
9 changes: 7 additions & 2 deletions platform/darwin/src/MGLNetworkConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ + (NSURLSessionConfiguration *)defaultSessionConfiguration {
- (NSURLSession *)sessionForNetworkManager:(MGLNativeNetworkManager *)networkManager {
// Note: this method is NOT called on the main thread.
NSURLSession *session;
if ([self.delegate conformsToProtocol:@protocol(MGLNetworkConfigurationSessionDelegate)]) {
session = [(id<MGLNetworkConfigurationSessionDelegate>)self.delegate sessionForNetworkConfiguration:self];
if ([self.delegate respondsToSelector:@selector(sessionForNetworkConfiguration:)]) {
session = [self.delegate sessionForNetworkConfiguration:self];
}

// Check for a background session; string checking is fragile, but this is not
Expand All @@ -79,6 +79,11 @@ - (NSURLSession *)sessionForNetworkManager:(MGLNativeNetworkManager *)networkMan
NSAssert(![session isKindOfClass:NSClassFromString(@"__NSURLBackgroundSession")],
@"Background NSURLSessions are not yet supported");

if (session.delegate) {
NSAssert(![session.delegate conformsToProtocol:@protocol(NSURLSessionDataDelegate)],
@"Session delegates conforming to NSURLSessionDataDelegate are not yet supported");
}

return session;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ + (void)testing_clearNativeNetworkManagerDelegate;
+ (id)testing_nativeNetworkManagerDelegate;
@end

@interface MGLNetworkConfigurationTestDelegate: NSObject <MGLNetworkConfigurationSessionDelegate>
@interface MGLNetworkConfigurationTestDelegate: NSObject <MGLNetworkConfigurationDelegate>
@property (nonatomic) NSURLSession *(^handler)();
@end

Expand Down Expand Up @@ -284,8 +284,16 @@ - (void)testFailureForNetworkConfigurationWithSessionWithDataDelegate🔒 {
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig
delegate:delegate
delegateQueue:nil];
[self internalTestNetworkConfigurationWithSession:session shouldDownload:YES];

BOOL conforms = [session.delegate conformsToProtocol:@protocol(NSURLSessionDataDelegate)];
XCTAssert(conforms);
#ifdef DEBUG
if (conforms) {
NSLog(@"Session delegates conforming to NSURLSessionDataDelegate are not supported");
}
#else
[self internalTestNetworkConfigurationWithSession:session shouldDownload:YES];
#endif
[session finishTasksAndInvalidate];

XCTAssertFalse(didCallReceiveData);
Expand Down

0 comments on commit 9d2efab

Please sign in to comment.