Merge branch 'bugfix/qos_async' into 'develop'
Bugfix/qos async ### Summary Shift thread blocking functions to swift async/await and fix QOS warnings. Co-authored-by: Scott Pfeil <Scott.Pfeil3@verizonwireless.com> See merge request https://gitlab.verizon.com/BPHV_MIPS/mvm_core/-/merge_requests/277
This commit is contained in:
commit
9419fd51a2
@ -97,6 +97,7 @@
|
||||
AF60A7F4289212EB00919EEB /* MVMCoreError.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF60A7F3289212EB00919EEB /* MVMCoreError.swift */; };
|
||||
AF686FDA2A8A876A008F666A /* NavigationOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF686FD92A8A876A008F666A /* NavigationOperation.swift */; };
|
||||
AF686FDE2A8D29CD008F666A /* MVMCoreLoadRequestOperation+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF686FDD2A8D29CD008F666A /* MVMCoreLoadRequestOperation+Extension.swift */; };
|
||||
AF6870292A9CD0E9008F666A /* MVMCoreLoadHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF6870282A9CD0E9008F666A /* MVMCoreLoadHandler.swift */; };
|
||||
AF69D4E9286E54D500BC6862 /* ActionCallHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF69D4E8286E54D500BC6862 /* ActionCallHandler.swift */; };
|
||||
AF69D4EB286E586200BC6862 /* ActionRestartHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF69D4EA286E586200BC6862 /* ActionRestartHandler.swift */; };
|
||||
AF69D4ED286E5D8C00BC6862 /* ActionCancelHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF69D4EC286E5D8C00BC6862 /* ActionCancelHandler.swift */; };
|
||||
@ -252,6 +253,7 @@
|
||||
AF60A7F3289212EB00919EEB /* MVMCoreError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MVMCoreError.swift; sourceTree = "<group>"; };
|
||||
AF686FD92A8A876A008F666A /* NavigationOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationOperation.swift; sourceTree = "<group>"; };
|
||||
AF686FDD2A8D29CD008F666A /* MVMCoreLoadRequestOperation+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MVMCoreLoadRequestOperation+Extension.swift"; sourceTree = "<group>"; };
|
||||
AF6870282A9CD0E9008F666A /* MVMCoreLoadHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MVMCoreLoadHandler.swift; sourceTree = "<group>"; };
|
||||
AF69D4E8286E54D500BC6862 /* ActionCallHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionCallHandler.swift; sourceTree = "<group>"; };
|
||||
AF69D4EA286E586200BC6862 /* ActionRestartHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionRestartHandler.swift; sourceTree = "<group>"; };
|
||||
AF69D4EC286E5D8C00BC6862 /* ActionCancelHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionCancelHandler.swift; sourceTree = "<group>"; };
|
||||
@ -560,6 +562,7 @@
|
||||
AFBB96371FBA39E70008D868 /* MVMCoreLoadDelegateProtocol.h */,
|
||||
AFBB96391FBA3A550008D868 /* MVMCoreLoadHandler.h */,
|
||||
AFBB964B1FBA3A560008D868 /* MVMCoreLoadHandler.m */,
|
||||
AF6870282A9CD0E9008F666A /* MVMCoreLoadHandler.swift */,
|
||||
AFBB964A1FBA3A560008D868 /* MVMCoreLoadRequestOperation.h */,
|
||||
AFBB96521FBA3A570008D868 /* MVMCoreLoadRequestOperation.m */,
|
||||
AF686FDD2A8D29CD008F666A /* MVMCoreLoadRequestOperation+Extension.swift */,
|
||||
@ -919,6 +922,7 @@
|
||||
AF7069A02880F0EB00077CF6 /* ActionOpenPageHandler.swift in Sources */,
|
||||
AFBB96B11FBA3B590008D868 /* MVMCoreDispatchUtility.m in Sources */,
|
||||
946EE1A3237B59C30036751F /* ModelProtocol.swift in Sources */,
|
||||
AF6870292A9CD0E9008F666A /* MVMCoreLoadHandler.swift in Sources */,
|
||||
AFEA17A9209B6A1C00BC6740 /* MVMCoreBlockOperation.m in Sources */,
|
||||
AF43A70A1FC4F415008E9347 /* MVMCoreCache.m in Sources */,
|
||||
AF43A6FF1FBE3252008E9347 /* Reachability.m in Sources */,
|
||||
|
||||
@ -48,13 +48,18 @@
|
||||
- (void)setHeadersForRequest:(nonnull NSMutableURLRequest *)request requestParameters:(nonnull MVMCoreRequestParameters *)requestParameters;
|
||||
|
||||
// Converts the core request parameters into a JSON object dictionary.
|
||||
- (nonnull NSDictionary *)convertToJsonDictionary:(nonnull MVMCoreRequestParameters *)requestParameters;
|
||||
- (void)getJsonDictionary:(nonnull MVMCoreRequestParameters *)requestParameters closure:(nonnull void (^)(NSDictionary * _Nullable jsonDictionary))closure;
|
||||
|
||||
- (void)getJsonData:(nonnull MVMCoreRequestParameters *)requestParameters forUrl:(nonnull NSURL *)url closure:(nonnull void (^)(NSData * _Nullable data, MVMCoreErrorObject *_Nullable error))closure;
|
||||
|
||||
- (void)getInitialParametersExcludingSections:(NSSet<NSString *> *_Nullable)excludeSections closure:(nonnull void (^)(NSDictionary *_Nullable parameters))closure;
|
||||
|
||||
// Creates a request object with the given parameters.
|
||||
- (nullable NSURLRequest *)requestWithParameters:(nonnull MVMCoreRequestParameters *)requestParameters error:(MVMCoreErrorObject *_Nullable *_Nonnull)error;
|
||||
- (void)transformToRequestWithParameters:(nonnull MVMCoreRequestParameters *)requestParameters closure:(nonnull void (^)(NSURLRequest * _Nullable request, MVMCoreErrorObject *_Nullable error))closure;
|
||||
|
||||
// Sends a given request to the server. When it is finished, it calls request finished, passing along the json object or nil if there is an error.
|
||||
- (nullable NSURLSessionTask *)sendRequest:(nonnull MVMCoreRequestParameters *)requestParameters locationForError:(nonnull NSString *)locationForError requestFinished:(nullable void (^)(id _Nullable jsonObject, MVMCoreErrorObject * _Nullable error))requestFinished;
|
||||
- (void)sendRequest:(nonnull MVMCoreRequestParameters *)requestParameters locationForError:(nonnull NSString *)locationForError requestFinished:(nullable void (^)(id _Nullable jsonObject, MVMCoreErrorObject * _Nullable error))requestFinished;
|
||||
- (nullable NSURLSessionTask *)sendRequest:(nonnull NSURLRequest *)request requestParameters:(nonnull MVMCoreRequestParameters *)requestParameters locationForError:(nonnull NSString *)locationForError requestFinished:(nullable void (^)(id _Nullable jsonObject, MVMCoreErrorObject *_Nullable error))requestFinished;
|
||||
|
||||
#pragma mark - Loading Functions
|
||||
|
||||
|
||||
@ -132,7 +132,7 @@
|
||||
|
||||
#pragma mark - Request Functions.
|
||||
|
||||
- (nullable NSURLRequest *)requestWithParameters:(nonnull MVMCoreRequestParameters *)requestParameters error:(MVMCoreErrorObject *_Nullable *_Nonnull)error {
|
||||
- (void)transformToRequestWithParameters:(nonnull MVMCoreRequestParameters *)requestParameters closure:(nonnull void (^)(NSURLRequest * _Nullable request, MVMCoreErrorObject *_Nullable error))closure {
|
||||
|
||||
NSURL *url = [requestParameters resolveURL:[MVMCoreSessionObject sharedGlobal]];
|
||||
|
||||
@ -152,26 +152,15 @@
|
||||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:timeOutInterval];
|
||||
[self setHeadersForRequest:request requestParameters:requestParameters];
|
||||
|
||||
MVMCoreErrorObject *jsonError;
|
||||
NSData *jsonData = [self convertToJsonData:requestParameters forUrl:url error:&jsonError];
|
||||
if (!jsonData) {
|
||||
*error = jsonError;
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSError *bodyError;
|
||||
NSData *body = jsonData;
|
||||
if (requestParameters.imageData) {
|
||||
body = [self createMultipartFormBodyForRequestJsonData:jsonData imageData:requestParameters.imageData error:&bodyError];
|
||||
}
|
||||
if (body) {
|
||||
[request setHTTPBody:body];
|
||||
} else {
|
||||
*error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:nil code:ErrorCodeParsingJSON domain:ErrorDomainNative location:[NSString stringWithFormat:@"requestWithParameters_%@",requestParameters.pageType]];
|
||||
return nil;
|
||||
}
|
||||
|
||||
return request;
|
||||
[self getJsonData:requestParameters forUrl:url closure:^(NSData * _Nullable jsonData, MVMCoreErrorObject * _Nullable error) {
|
||||
/// Ensures that something in the body is always set.
|
||||
if (!jsonData) {
|
||||
closure(nil, error ?: [[MVMCoreErrorObject alloc] initWithTitle:nil message:nil code:ErrorCodeParsingJSON domain:ErrorDomainNative location:[NSString stringWithFormat:@"requestWithParameters_%@",requestParameters.pageType]]);
|
||||
return;
|
||||
}
|
||||
[request setHTTPBody:jsonData];
|
||||
closure(request, nil);
|
||||
}];
|
||||
}
|
||||
|
||||
- (NSString *)boundaryForMultipartRequest {
|
||||
@ -202,64 +191,67 @@
|
||||
}
|
||||
}
|
||||
|
||||
- (nonnull NSDictionary *)convertToJsonDictionary:(nonnull MVMCoreRequestParameters *)requestParameters {
|
||||
- (void)getInitialParametersExcludingSections:(NSSet<NSString *> *_Nullable)excludeSections closure:(nonnull void (^)(NSDictionary *_Nullable parameters))closure {}
|
||||
|
||||
- (void)getJsonDictionary:(nonnull MVMCoreRequestParameters *)requestParameters closure:(nonnull void (^)(NSDictionary * _Nullable jsonDictionary))closure {
|
||||
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
|
||||
|
||||
// Sets up the Initial parameters.
|
||||
if (requestParameters.addInitialRequestParameters) {
|
||||
NSDictionary *initialParameters = [[MVMCoreSessionObject sharedGlobal] getInitialParametersExcludingSections:requestParameters.excludedInitialParameters];
|
||||
if (initialParameters) {
|
||||
[parameters setObject:initialParameters forKey:@"InitialParams"];
|
||||
}
|
||||
}
|
||||
|
||||
// Adds request specific parameters
|
||||
if (requestParameters.parameters.count) {
|
||||
[parameters setObject:requestParameters.parameters forKey:@"RequestParams"];
|
||||
}
|
||||
|
||||
return parameters;
|
||||
if (!requestParameters.addInitialRequestParameters) {
|
||||
closure(parameters);
|
||||
return;
|
||||
}
|
||||
|
||||
// Sets up the Initial parameters.
|
||||
[self getInitialParametersExcludingSections:requestParameters.excludedInitialParameters closure:^(NSDictionary * _Nullable initialParameters) {
|
||||
if (initialParameters) {
|
||||
[parameters setObject:initialParameters forKey:@"InitialParams"];
|
||||
}
|
||||
closure(parameters);
|
||||
}];
|
||||
}
|
||||
|
||||
- (nullable NSData *)convertToJsonData:(nonnull MVMCoreRequestParameters *)requestParameters forUrl:(nonnull NSURL *)url error:(MVMCoreErrorObject *_Nullable *_Nullable)error {
|
||||
NSDictionary *parameters = [self convertToJsonDictionary:requestParameters];
|
||||
|
||||
// Ensure the parameters are valid json.
|
||||
if (![NSJSONSerialization isValidJSONObject:parameters]) {
|
||||
if (error) {
|
||||
*error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeParsingJSON domain:ErrorDomainNative location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]];
|
||||
- (void)getJsonData:(nonnull MVMCoreRequestParameters *)requestParameters forUrl:(nonnull NSURL *)url closure:(nonnull void (^)(NSData * _Nullable data, MVMCoreErrorObject * _Nullable error))closure {
|
||||
[self getJsonDictionary:requestParameters closure:^(NSDictionary * _Nullable parameters) {
|
||||
// Ensure the parameters are valid json.
|
||||
if (![NSJSONSerialization isValidJSONObject:parameters]) {
|
||||
closure(nil, [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeParsingJSON domain:ErrorDomainNative location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]);
|
||||
return;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSError *jsonError = nil;
|
||||
NSData *data = nil;
|
||||
#if LOGGING
|
||||
// Pretty print for logging the request parameters.
|
||||
data = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&jsonError];
|
||||
if (!data) {
|
||||
if (error) {
|
||||
*error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] messageToLog:jsonError.localizedDescription code:jsonError.code domain:ErrorDomainSystem location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]];
|
||||
|
||||
NSError *jsonError = nil;
|
||||
NSData *data = nil;
|
||||
#if LOGGING
|
||||
// Pretty print for logging the request parameters.
|
||||
data = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&jsonError];
|
||||
if (!data) {
|
||||
closure(nil, [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] messageToLog:jsonError.localizedDescription code:jsonError.code domain:ErrorDomainSystem location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]);
|
||||
return;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||
MVMCoreLog(@"Request Parameters for URL %@:\n%@", [url absoluteString], jsonString);
|
||||
#endif
|
||||
|
||||
// Standard condensed to send to the server.
|
||||
data = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&jsonError];
|
||||
if (!data) {
|
||||
if (error) {
|
||||
*error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] messageToLog:jsonError.localizedDescription code:jsonError.code domain:ErrorDomainSystem location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]];
|
||||
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||
MVMCoreLog(@"Request Parameters for URL %@:\n%@", [url absoluteString], jsonString);
|
||||
#endif
|
||||
|
||||
// Standard condensed to send to the server.
|
||||
data = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&jsonError];
|
||||
if (!data) {
|
||||
closure(nil, [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] messageToLog:jsonError.localizedDescription code:jsonError.code domain:ErrorDomainSystem location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]);
|
||||
return;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
return data;
|
||||
|
||||
// Add any image data
|
||||
if (requestParameters.imageData) {
|
||||
data = [self createMultipartFormBodyForRequestJsonData:data imageData:requestParameters.imageData];
|
||||
}
|
||||
closure(data, nil);
|
||||
}];
|
||||
}
|
||||
|
||||
- (nonnull NSData *)createMultipartFormBodyForRequestJsonData:(nonnull NSData *)jsonData imageData:(nonnull NSData *)imageData error:(NSError *_Nullable *_Nullable)error {
|
||||
- (nonnull NSData *)createMultipartFormBodyForRequestJsonData:(nonnull NSData *)jsonData imageData:(nonnull NSData *)imageData {
|
||||
|
||||
NSMutableData *body = [[NSMutableData alloc] init];
|
||||
|
||||
@ -280,7 +272,19 @@
|
||||
return body;
|
||||
}
|
||||
|
||||
- (nullable NSURLSessionTask *)sendRequest:(nonnull MVMCoreRequestParameters *)requestParameters locationForError:(nonnull NSString *)locationForError requestFinished:(nullable void (^)(id _Nullable jsonObject, MVMCoreErrorObject *_Nullable error))requestFinished {
|
||||
- (void)sendRequest:(nonnull MVMCoreRequestParameters *)requestParameters locationForError:(nonnull NSString *)locationForError requestFinished:(nullable void (^)(id _Nullable jsonObject, MVMCoreErrorObject *_Nullable error))requestFinished {
|
||||
[self transformToRequestWithParameters:requestParameters closure:^(NSURLRequest * _Nullable request, MVMCoreErrorObject * _Nullable error) {
|
||||
if (error) {
|
||||
if (requestFinished) {
|
||||
requestFinished(nil, error);
|
||||
}
|
||||
return;
|
||||
}
|
||||
[self sendRequest:request requestParameters:requestParameters locationForError:locationForError requestFinished:requestFinished];
|
||||
}];
|
||||
}
|
||||
|
||||
- (nullable NSURLSessionTask *)sendRequest:(nonnull NSURLRequest *)request requestParameters:(nonnull MVMCoreRequestParameters *)requestParameters locationForError:(nonnull NSString *)locationForError requestFinished:(nullable void (^)(id _Nullable jsonObject, MVMCoreErrorObject *_Nullable error))requestFinished {
|
||||
|
||||
#if ENABLE_HARD_CODED_RESPONSE
|
||||
NSDictionary *response = [[MFHardCodedServerResponse sharedInstance] getHardCodedResponseForRequest:requestParameters];
|
||||
@ -292,22 +296,15 @@
|
||||
}
|
||||
#endif
|
||||
|
||||
MVMCoreErrorObject *error = nil;
|
||||
NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];
|
||||
NSURLRequest *request = [self requestWithParameters:requestParameters error:&error];
|
||||
if (!request) {
|
||||
if (requestFinished) {
|
||||
requestFinished(nil,error);
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSURLSession *session = [MVMCoreSessionObject sharedGlobal].session;
|
||||
if (!session) {
|
||||
#warning Hotfix for when session is nil. We should switch to logging these errors once we fix how session handler misses canceling certain calls because they happen at the same time.
|
||||
error = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorCritical] messageToLog:nil code:ErrorCodeDefault domain:ErrorDomainNative location:locationForError];
|
||||
MVMCoreErrorObject *error = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorCritical] messageToLog:nil code:ErrorCodeDefault domain:ErrorDomainNative location:locationForError];
|
||||
error.logError = NO;
|
||||
requestFinished(nil,error);
|
||||
if (requestFinished) {
|
||||
requestFinished(nil, error);
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
MVMCoreLog(@"********************************* Cookie Sent *********************************");
|
||||
@ -316,7 +313,7 @@
|
||||
}];
|
||||
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:MVMCoreNotificationGoingToServer object:nil];
|
||||
|
||||
NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];
|
||||
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
||||
|
||||
MVMCoreLog(@"Request Time %f", [NSDate timeIntervalSinceReferenceDate] - startTime);
|
||||
|
||||
25
MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.swift
Normal file
25
MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.swift
Normal file
@ -0,0 +1,25 @@
|
||||
//
|
||||
// MVMCoreLoadHandler.swift
|
||||
// MVMCore
|
||||
//
|
||||
// Created by Scott Pfeil on 8/28/23.
|
||||
// Copyright © 2023 myverizon. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension MVMCoreLoadHandler {
|
||||
func sendRequest(with parameters: MVMCoreRequestParameters, locationForError: String, requestFinished: @escaping (Any?, MVMCoreErrorObject?) -> Void) async -> URLSessionTask? {
|
||||
return await withCheckedContinuation { continuation in
|
||||
transformToRequest(with: parameters) { request, error in
|
||||
guard let request = request else {
|
||||
defer {
|
||||
requestFinished(nil, error)
|
||||
}
|
||||
return continuation.resume(returning: nil)
|
||||
}
|
||||
continuation.resume(returning: self.send(request, requestParameters: parameters, locationForError: locationForError, requestFinished: requestFinished))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -304,29 +304,31 @@
|
||||
}
|
||||
#endif
|
||||
|
||||
loadObject.operation.sessionTask = [[MVMCoreLoadHandler sharedGlobal] sendRequest:requestParameters locationForError:[[MVMCoreLoadHandler sharedGlobal] errorLocationForRequest:loadObject] requestFinished:^(id jsonObject, MVMCoreErrorObject *error) {
|
||||
|
||||
if ([loadObject.operation checkAndHandleForCancellation]) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (jsonObject) {
|
||||
[[MVMCoreLoadHandler sharedGlobal] transformToRequestWithParameters:requestParameters closure:^(NSURLRequest * _Nullable request, MVMCoreErrorObject * _Nullable error) {
|
||||
loadObject.operation.sessionTask = [[MVMCoreLoadHandler sharedGlobal] sendRequest:request requestParameters:requestParameters locationForError:[[MVMCoreLoadHandler sharedGlobal] errorLocationForRequest:loadObject] requestFinished:^(id jsonObject, MVMCoreErrorObject *error) {
|
||||
|
||||
if ([jsonObject isKindOfClass:[NSDictionary class]]) {
|
||||
|
||||
completionHandler(jsonObject);
|
||||
|
||||
} else {
|
||||
|
||||
// Error json not correct format.
|
||||
MVMCoreErrorObject *errorObject = [[MVMCoreLoadHandler sharedGlobal] errorForLoadObject:loadObject withTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorCritical] code:ErrorCodeJSONNotDictionary domain:ErrorDomainSystem];
|
||||
[MVMCoreLoadRequestOperation loadAbortedWithError:errorObject loadObject:loadObject];
|
||||
if ([loadObject.operation checkAndHandleForCancellation]) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// Error with the request/response
|
||||
[[MVMCoreLoadHandler sharedGlobal] attachLoadInformation:loadObject toError:error];
|
||||
[MVMCoreLoadRequestOperation loadAbortedWithError:error loadObject:loadObject];
|
||||
}
|
||||
|
||||
if (jsonObject) {
|
||||
|
||||
if ([jsonObject isKindOfClass:[NSDictionary class]]) {
|
||||
|
||||
completionHandler(jsonObject);
|
||||
|
||||
} else {
|
||||
|
||||
// Error json not correct format.
|
||||
MVMCoreErrorObject *errorObject = [[MVMCoreLoadHandler sharedGlobal] errorForLoadObject:loadObject withTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorCritical] code:ErrorCodeJSONNotDictionary domain:ErrorDomainSystem];
|
||||
[MVMCoreLoadRequestOperation loadAbortedWithError:errorObject loadObject:loadObject];
|
||||
}
|
||||
} else {
|
||||
// Error with the request/response
|
||||
[[MVMCoreLoadHandler sharedGlobal] attachLoadInformation:loadObject toError:error];
|
||||
[MVMCoreLoadRequestOperation loadAbortedWithError:error loadObject:loadObject];
|
||||
}
|
||||
}];
|
||||
}];
|
||||
}
|
||||
|
||||
|
||||
@ -46,10 +46,10 @@
|
||||
completionHandler(nil)
|
||||
return
|
||||
}
|
||||
try getParameters(with: clientParameterModel, requestParameters: requestParameters, actionId: actionId, completionHandler: completionHandler)
|
||||
getParameters(with: clientParameterModel, requestParameters: requestParameters, actionId: actionId, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
open func getParameters(with model: ClientParameterModel, requestParameters: [String: Any], actionId: String, completionHandler:@escaping ([String: Any]?) -> ()) throws {
|
||||
open func getParameters(with model: ClientParameterModel, requestParameters: [String: Any], actionId: String, completionHandler:@escaping ([String: Any]?) -> ()) {
|
||||
|
||||
let parametersWorkQueue = DispatchQueue(label: "com.mva.clientparameter")
|
||||
let group = DispatchGroup()
|
||||
|
||||
@ -32,9 +32,6 @@
|
||||
/// Gets inital parameters for request parameters.
|
||||
- (nullable NSDictionary *)getInitialParameters;
|
||||
|
||||
/// Gets inital parameters for request parameters excluding any items given a list of key names.
|
||||
- (nullable NSDictionary *)getInitialParametersExcludingSections:(nullable NSSet<NSString *> *)excludeSections;
|
||||
|
||||
/// Restarts the application session state. Can clear variables and pass a page type if needed.
|
||||
- (void)restartSessionWithPageType:(nullable NSString *)pageType requestUrl:(nullable NSURL *)requestUrl parameters:(nullable NSDictionary *)parameters clearAllVariables:(BOOL)clearAllVariables;
|
||||
|
||||
|
||||
@ -28,14 +28,6 @@
|
||||
return [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:nil delegateQueue:nil];
|
||||
}
|
||||
|
||||
- (nullable NSDictionary *)getInitialParameters {
|
||||
return [self getInitialParametersExcludingSections:nil];
|
||||
}
|
||||
|
||||
- (nullable NSDictionary *)getInitialParametersExcludingSections:(nullable NSSet<NSString *> *)excludeSections {
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)restartSessionWithPageType:(nullable NSString *)pageType requestUrl:(nullable NSURL *)requestUrl parameters:(nullable NSDictionary *)parameters clearAllVariables:(BOOL)clearAllVariables {
|
||||
// Clears the singletons of any session related data.
|
||||
if (clearAllVariables) {
|
||||
|
||||
@ -6,6 +6,11 @@
|
||||
// Copyright © 2023 myverizon. All rights reserved.
|
||||
//
|
||||
|
||||
public enum MVMCoreActionUtilityError: Swift.Error {
|
||||
case noTasks
|
||||
case timeOut
|
||||
}
|
||||
|
||||
public extension MVMCoreActionUtility {
|
||||
static func fatalClassCheck<T>(object: AnyObject?) -> T {
|
||||
guard let object = object else {
|
||||
@ -16,6 +21,44 @@ public extension MVMCoreActionUtility {
|
||||
}
|
||||
return instance
|
||||
}
|
||||
|
||||
/** Awaits and performs the operation if within the timeout time.
|
||||
Will time out the operation and throw ``MVMCoreActionUtilityError/timeOut`` if the operation exceeds the timeout time.
|
||||
- Parameter timemout: the time the operation has to finish before throwing a timeout error.
|
||||
- Parameter operation: the operation to perform.
|
||||
*/
|
||||
static func perform<T>(with timeout: Int = 1, operation: @escaping @Sendable () async throws -> T) async throws -> T {
|
||||
return try await withCheckedThrowingContinuation { continuation in
|
||||
Task {
|
||||
await withThrowingTaskGroup(of: T.self) { group in
|
||||
// Task for operation.
|
||||
group.addTask {
|
||||
try Task.checkCancellation()
|
||||
return try await operation()
|
||||
}
|
||||
|
||||
// Task for time out.
|
||||
group.addTask {
|
||||
try Task.checkCancellation()
|
||||
try await Task.sleep(nanoseconds: UInt64(timeout) * NSEC_PER_SEC)
|
||||
throw MVMCoreActionUtilityError.timeOut
|
||||
}
|
||||
|
||||
do {
|
||||
guard let result = try await group.next() else {
|
||||
continuation.resume(throwing: MVMCoreActionUtilityError.noTasks)
|
||||
return
|
||||
}
|
||||
group.cancelAll()
|
||||
continuation.resume(returning: result)
|
||||
} catch {
|
||||
group.cancelAll()
|
||||
continuation.resume(throwing: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
} as T
|
||||
}
|
||||
}
|
||||
|
||||
@objc public extension MVMCoreActionUtility {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user