Creative SDK Image component - Image Editor UI - ios

I completed all step of Creative SDK Image component .But,how can i convert this block into Swift language..
id<AdobeImageEditorRender> render = [photoEditor enqueueHighResolutionRenderWithImage:highResImage
completion:^(UIImage *result, NSError *error) {
if (result) {
} else {
}
}];
// Provide a block to receive updates about the status of the render
[render setProgressHandler:^(CGFloat progress) {
print("Do something")
}];

It may not be exactly this, since I don't have the SDK installed, but it should be very close to this:
let render = photoEditor.enqueueHighResolutionRenderWithImage(image) { result, error in
if let result = result {
// do something with result.
} else {
// do something with error.
}
}
render.progressHandler = { progress in
// update progress, if
}
if you reference self in the blocks, you need to put [unowned self] after the brace like this:
render.progressHandler = { [unowned self] progress in
self.updateProgress(progress)
}

Related

How repeat part of function Swift

still learning the basics. I have a function in which there is a block, that needs to be repeated without calling the whole function again. How is this done in Swift?
func connected(to peripheral: Peripheral) {
let cwConnection = CWStatusBarNotification()
cwConnection.display(withMessage: "Ring Connected", forDuration: 3)
BluejayManager.shared.getHeliosInfo { (success) in
if success {
// Go on
} else {
// Repeat this block (BluejayManager.shared.getHeliosInfo)
}
}
}
Hey Riyan it's just simple. Here is the solution to your problem. Just put the block in other small method and when you just need to call that block call that small function.
func connected(to peripheral: Peripheral) {
let cwConnection = CWStatusBarNotification()
cwConnection.display(withMessage: "Ring Connected", forDuration: 3)
self.callBluejayManagerShared() // Call of block from method
}
func callBluejayManagerShared(){
BluejayManager.shared.getHeliosInfo { (success) in
if success {
// Go on
} else {
// Repeat this block (BluejayManager.shared.getHeliosInfo)
self.callBluejayManagerShared()
}
}
}
Now when you just want to call block you just need to call self.callBluejayManagerShared() method.
Hope this help you
You can use repeat - while around and BluejayManager.shared.getHeliosInfo check for success as break condition:
repeatGetInfo: repeat {
BluejayManager.shared.getHeliosInfo
{ (success) in
if success
{
// do your stuff.
break repeatGetInfo
} else
{
continue repeatGetInfo
}
}
} while true
Hope this helps

Converting a block from Objective-C to Swift

I have written the following method that returns a block that I've written in Objective-C. No matter how many times I mess with the syntax I can't get a swift version of this method that the compiler likes.
- (TWCInviteAcceptanceBlock)acceptHandler
{
return ^(TWCConversation * _Nullable conversation, NSError * _Nullable error) {
if (conversation) {
NSLog("Yay")
}
else {
NSLog(#"Boo")
}
};
}
Any ideas?
Off the top of my head:
func acceptHandler() -> TWCInviteAcceptanceBlock {
return { (conversation: TWCConversation?, error: NSError?) in
if let conversation = conversation {
print("Yay")
} else {
print("Boo")
}
}
}

Error Handling When Saving a CKRecord

I am looking for an example of proper error handling when saving a CKRecord. According to the Apple docs I should "Use the information in the error object to determine whether a problem has a workaround."
I understand that the error object has a userInfo dictionary, but how do I figure out what the keys are for the dictionary and how to handle the errors?
The following example illustrates how I'm currently saving a CKRecord:
CKRecord *record = [[CKRecord alloc] initWithRecordType:#"MyRecordType"];
[record setValue:[NSNumber numberWithInt:99] forKey:#"myInt"];
[db saveRecord:record completionHandler:^(CKRecord *savedPlace, NSError *error) {
// handle errors here
if (savedPlace) {
NSLog(#"save successful");
}else{
NSLog(#"save unsuccessful");
}
if (error) {
NSLog(#"Error saving %#", error.localizedDescription);
}
}];
How can I improve this code to provide work arounds for potential saving issues?
In my library EVCloudKitDao I have a separate method that will return a error type based on the error code. Depending on that type you can decide what to do. Here is that method:
public enum HandleCloudKitErrorAs {
case Success,
Retry(afterSeconds:Double),
RecoverableError,
Fail
}
public static func handleCloudKitErrorAs(error:NSError?, retryAttempt:Double = 1) -> HandleCloudKitErrorAs {
if error == nil {
return .Success
}
let errorCode:CKErrorCode = CKErrorCode(rawValue: error!.code)!
switch errorCode {
case .NetworkUnavailable, .NetworkFailure, .ServiceUnavailable, .RequestRateLimited, .ZoneBusy, .ResultsTruncated:
// Use an exponential retry delay which maxes out at half an hour.
var seconds = Double(pow(2, Double(retryAttempt)))
if seconds > 1800 {
seconds = 1800
}
// Or if there is a retry delay specified in the error, then use that.
if let userInfo = error?.userInfo {
if let retry = userInfo[CKErrorRetryAfterKey] as? NSNumber {
seconds = Double(retry)
}
}
NSLog("Debug: Should retry in \(seconds) seconds. \(error)")
return .Retry(afterSeconds: seconds)
case .UnknownItem, .InvalidArguments, .IncompatibleVersion, .BadContainer, .MissingEntitlement, .PermissionFailure, .BadDatabase, .AssetFileNotFound, .OperationCancelled, .NotAuthenticated, .AssetFileModified, .BatchRequestFailed, .ZoneNotFound, .UserDeletedZone, .InternalError, .ServerRejectedRequest, .ConstraintViolation:
NSLog("Error: \(error)")
return .Fail;
case .QuotaExceeded, .LimitExceeded:
NSLog("Warning: \(error)")
return .Fail;
case .ChangeTokenExpired, .ServerRecordChanged:
NSLog("Info: \(error)")
return .RecoverableError
default:
NSLog("Error: \(error)") //New error introduced in iOS...?
return .Fail;
}
}
Inside the callback of a CloudKit method you can then use this function like this:
func loadContacts(retryCount:Double = 1) {
// Look who of our contact is also using this app.
EVCloudKitDao.publicDB.allContactsUserInfo({ users in
EVLog("AllContactUserInfo count = \(users.count)");
Async.main{
self.contacts = users
self.tableView.reloadData()
}
}, errorHandler: { error in
switch EVCloudKitDao.handleCloudKitErrorAs(error, retryAttempt: retryCount) {
case .Retry(let timeToWait):
Async.background(after: timeToWait) {
self.loadContacts(retryCount + 1)
}
case .Fail:
Helper.showError("Something went wrong: \(error.localizedDescription)")
default: // For here there is no need to handle the .Success, .Fail and .RecoverableError
break
}
})
}
In my the case above I use a separate error callback handler. You can also call it directly form within a CloudKit method callback. Just first check if there is an error.
Here is an implementation in which I handle the common error of CKErrorNetworkFailure by retrying to save after the recommended retry after time interval which is stored in the userInfo dictionary.
-(void)saveRecord:(CKRecord*)record toDatabase:(CKDatabase*)database{
[database saveRecord:record completionHandler:^(CKRecord *record, NSError *error) {
if (error==nil) {
NSLog(#"The save was successful");
//Do something
}else{
NSLog(#"Error saving with localizedDescription: %#", error.localizedDescription);
NSLog(#"CKErrorCode = %lu", [error code]);
if ([error code]==CKErrorNetworkFailure) {
double retryAfterValue = [[error.userInfo valueForKey:CKErrorRetryAfterKey] doubleValue];
NSLog(#"Error code network unavailable retrying after %f", retryAfterValue);
NSTimer *timer = [NSTimer timerWithTimeInterval:retryAfterValue target:self selector:#selector(testOutCloudKit) userInfo:nil repeats:NO];
[timer fire];
}
}
}];
}
In Swift 3, I handle my CloudKit errors this way:
privateDB.perform(myQuery, inZoneWith: nil) {records, error in
if error != nil {
print (error?.localizedDescription)
if error?._code == CKError.notAuthenticated.rawValue {
// Solve problems here...
}
Note that the CKError.notAuthenticated is selected from the list presented by code completion after writing CKError.
Hope this helps!

Using PromiseKit to force sequential download

I am using PromiseKit and would like to force sequential download of JSONs. The count of JSONs might change.
I have read this about chaining.
If I had a fixed number of say 3 downloads, this would be fine.
But what if I had a changing count of download that I would like to download sequentially?
This is my code for 2 URLs. I wonder how I could do this with dateUrlArray[i] iteration over the array?
- (void)downloadJSONWithPromiseKitDateArray:(NSMutableArray *)dateUrlArray {
[self.operationManager GET:dateUrlArray[0]
parameters:nil]
.then(^(id responseObject, AFHTTPRequestOperation *operation) {
NSDictionary *resultDictionary = (NSDictionary *) responseObject;
Menu *menu = [JsonMapper mapMenuFromDictionary:resultDictionary];
if (menu) {
[[DataAccess instance] addMenuToRealm:menu];
}
return [self.operationManager GET:dateUrlArray[1]
parameters:nil];
}).then(^(id responseObject, AFHTTPRequestOperation *operation) {
NSDictionary *resultDictionary = (NSDictionary *) responseObject;
Menu *menu = [JsonMapper mapMenuFromDictionary:resultDictionary];
if (menu) {
[[DataAccess instance] addMenuToRealm:menu];
}
})
.catch(^(NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[self handleCatchwithError:error];
});
}).finally(^{
dispatch_async(dispatch_get_main_queue(), ^{
DDLogInfo(#".....finally");
});
});
}
The concept you're looking for is thenable chaining. You want to chain multiple promises in a for loop.
My Objective-C is really rusty - but it should look something like:
// create an array for the results
__block NSMutableArray *results = [NSMutableArray arrayWithCapacity:[urls count]];
// create an initial promise
PMKPromise *p = [PMKPromise promiseWithValue: nil]; // create empty promise
for (id url in urls) {
// chain
p = p.then(^{
// chain the request and storate
return [self.operationManager GET:url
parameters:nil].then(^(id responseObject, AFHTTPRequestOperation *operation) {
[results addObject:responseObject]; // reference to result
return nil;
});
});
}
p.then(^{
// all results available here
});
For those of us looking for a Swift 2.3 solution:
import PromiseKit
extension Promise {
static func resolveSequentially(promiseFns: [()->Promise<T>]) -> Promise<T>? {
return promiseFns.reduce(nil) { (fn1: Promise<T>?, fn2: (()->Promise<T>)?) -> Promise<T>? in
return fn1?.then({ (_) -> Promise<T> in
return fn2!()
}) ?? fn2!()
}
}
}
Note that this function returns nil if the promises array is empty.
Example of use
Below is an example of how to upload an array of attachments in sequence:
func uploadAttachments(attachments: [Attachment]) -> Promise<Void> {
let promiseFns = attachments.map({ (attachment: Attachment) -> (()->Promise<Void>) in
return {
return self.uploadAttachment(attachment)
}
})
return Promise.resolveSequentially(promiseFns)?.then({}) ?? Promise()
}
func uploadAttachment(attachment: Attachment) -> Promise<Void> {
// Do the actual uploading
return Promise()
}
Thanks for Vegard's answer and I rewrite for Swift 3:
extension Promise {
static func resolveSequentially(promiseFns: [()->Promise<T>]) -> Promise<T>? {
return promiseFns.reduce(nil) { (fn1: Promise<T>?, fn2: (()->Promise<T>)?) -> Promise<T>? in
return fn1?.then{ (_) -> Promise<T> in
return fn2!()
} ?? fn2!()
}
}
}
/* Example */
func uploadAttachments(_ attachments: [Attachment]) -> Promise<Void> {
let promiseFns = attachments.map({ (attachment: Attachment) -> (()->Promise<Void>) in
return {
return self. uploadAttachment(attachment)
}
})
return Promise.resolveSequentially(promiseFns: promiseFns)?.then{Void -> Void in} ?? Promise { Void -> Void in }
}

Prevent a closure from running until another has completed

Here is code for two closures in two different IBAction button presses. The desired outcome is for the button press to turn on/off an LED, then to access a light sensor and read the light value after the change in LED status.
What happens is a race condition where the function getVariable runs and returns before the callFunction has implemented the change. The result is that the value displayed in getLightLabel.text is that of the prior condition, not the current condition.
My question is how to rewrite the code below so that myPhoton!.getVariable does not execute until after the myPhoton!.callFunction has returned (completed its task).
I have tried placing getVariable inside callFunction, both before and after the } closing if (error == nil), but the result was identical to the code shown here.
#IBAction func lightOn(sender: AnyObject) {
let funcArgs = [1]
myPhoton!.callFunction("lightLed0", withArguments: funcArgs) { (resultCode : NSNumber!, error : NSError!) -> Void in
if (error == nil) {
self.lightStateLabel.text = "LED is on"
}
}
myPhoton!.getVariable("Light", completion: { (result:AnyObject!, error:NSError!) -> Void in
if let e = error {
self.getLightLabel.text = "Failed reading light"
}
else {
if let res = result as? Float {
self.getLightLabel.text = "Light level is \(res) lumens"
}
}
})
}
#IBAction func lightOff(sender: AnyObject) {
let funcArgs = [0]
myPhoton!.callFunction("lightLed0", withArguments: funcArgs) { (resultCode : NSNumber!, error : NSError!) -> Void in
if (error == nil) {
self.lightStateLabel.text = "LED is off"
}
}
myPhoton!.getVariable("Light", completion: { (result:AnyObject!, error:NSError!) -> Void in
if let e = error {
self.getLightLabel.text = "Failed reading light"
}
else {
if let res = result as? Float {
self.getLightLabel.text = "Light level is \(res) lumens"
}
}
})
}
Here is the callFunction comments and code from the .h file. This SDK is written in Objective C. I am using it in Swift with a bridging header file.
/**
* Call a function on the device
*
* #param functionName Function name
* #param args Array of arguments to pass to the function on the device. Arguments will be converted to string maximum length 63 chars.
* #param completion Completion block will be called when function was invoked on device. First argument of block is the integer return value of the function, second is NSError object in case of an error invoking the function
*/
-(void)callFunction:(NSString *)functionName withArguments:(NSArray *)args completion:(void (^)(NSNumber *, NSError *))completion;
/*
-(void)addEventHandler:(NSString *)eventName handler:(void(^)(void))handler;
-(void)removeEventHandler:(NSString *)eventName;
*/
Here is the .m file code
-(void)callFunction:(NSString *)functionName withArguments:(NSArray *)args completion:(void (^)(NSNumber *, NSError *))completion
{
// TODO: check function name exists in list
NSURL *url = [self.baseURL URLByAppendingPathComponent:[NSString stringWithFormat:#"v1/devices/%#/%#", self.id, functionName]];
NSMutableDictionary *params = [NSMutableDictionary new]; //[self defaultParams];
// TODO: check response of calling a non existant function
if (args) {
NSMutableArray *argsStr = [[NSMutableArray alloc] initWithCapacity:args.count];
for (id arg in args)
{
[argsStr addObject:[arg description]];
}
NSString *argsValue = [argsStr componentsJoinedByString:#","];
if (argsValue.length > MAX_SPARK_FUNCTION_ARG_LENGTH)
{
// TODO: arrange user error/codes in a list
NSError *err = [self makeErrorWithDescription:[NSString stringWithFormat:#"Maximum argument length cannot exceed %d",MAX_SPARK_FUNCTION_ARG_LENGTH] code:1000];
if (completion)
completion(nil,err);
return;
}
params[#"args"] = argsValue;
}
[self setAuthHeaderWithAccessToken];
[self.manager POST:[url description] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (completion)
{
NSDictionary *responseDict = responseObject;
if ([responseDict[#"connected"] boolValue]==NO)
{
NSError *err = [self makeErrorWithDescription:#"Device is not connected" code:1001];
completion(nil,err);
}
else
{
// check
NSNumber *result = responseDict[#"return_value"];
completion(result,nil);
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
if (completion)
completion(nil,error);
}];
}
One solution is to put the second closure inside the first, where the first returns and provides and Error value. If no error,then execuet the second closure. That is one way to tightly couple the two closures without resorting to semaphores or other messaging schemes.
In this application, the problem I was encountering cannot be solved on the IOS/Swift side of the stack. The cloud API and embedded uP are not tightly coupled, so the cloud returns to the IOS with a completion before the full function code has run on the Particle uP.
The solution to this overall problem actually lies in either modifying the cloud API or adding some additional code to the uP firmware to tightly couple the process to the IOS app with additional communication.

Resources