Search Your Question

Showing posts with label Webservice. Show all posts
Showing posts with label Webservice. Show all posts

What is Object mapping that helping Jon parsing?

Ans. : Object mapping is convert to JSON to SomeOtherObject, BussinessModel to PersistenceModel, etc or map fields of JSON to SomeOtherObject fields.

There are many third party library available for same purpose.

1. We use mapping using init method without third party library :

struct User {
  let id: Int
  let name: String
  
  init?(dictionary: [String: Any]) {
    guard let id = dictionary["id"] as? Int else { return nil }
    guard let name = dictionary["user_name"] as? String else { return nil }
    
    self.id = id
    self.name = name
  }
}

do {
  if let userDictionary = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] {
    let user = User(dictionary: userDictionary)
    //Do something with user
  }
} catch {
  print(error)

}

3. Using codable protocol  (Without third party)
4. SwiftyJson 

In today's world mostly people using 3rd option for  object mapping.

Do you know SMTP? is it use SOAP or REST?

Ans : 

SMTP : 
SMTP (Simple Mail Transfer Protocol) is a TCP/IP protocol used in sending and receiving e-mail.

SOAP and REST both allow you to create your own API. API stands for Application Programming Interface. It makes it possible to transfer data from an application to other applications. An API receives requests and sends back responses through internet protocols such as HTTP, SMTP, and others.

It use SOAP web-service. SOAP is protocol and rules are maintained by World wide web consortium(W3C).

Difference between synchronous and asynchronous calls in Objective-C

Ans : 

Synchronous :
This call means task will be executed in order.
Asynchronous : This call means task may or may not be executed in order.

When call is called synchronously, then thread that initiated that operation will be wait to current task to be finished.
When call is called asynchronously, then it will not wait.

If we want to do some task without harassing UI, we can do those tasks in background thread. This goal is to keep free main thread, so it continuously respond UI event. So we can dispatch our task in background state asynchronously.

So for do task in background thread, we will divide in 2 parts.

1. GCD - Grand Central Dispatch. By using GCD, you have to grab one of global background queue or create your own background queue.

// one of the global concurrent background queues
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

// or you could create your own serial background queue:
// dispatch_queue_t queue = dispatch_queue_create("com.iosiqa.app.queuename", 0);

2. Dispatch your task to that queue asynchronously

dispatch_async(queue, ^{
    // task that to be done in background and it may be slow
});

The pattern for operation queues is very similar. Create an operation queue and add operations to that queue.

Lets see example :

Asynchronous call with Multithreading :

// Methods gets called in different thread and does not block the current thread.
[NSURLConnection sendAsynchronousRequest:request
                                   queue:queue
                       completionHandler:
    ^(NSURLResponse *response, NSData *data, NSError *error) {
}];

Synchronous call with Multithreading (not so useful):

//Do something
dispatch_sync(queue, ^{
    //Do something else // work in another queue or thread
});

//Do some more task


Difference between thread-safe and non-thread-safe in iOS

Ans : 

Thread-Unsafe -> If any object allow to modify by more than one thread at the same time.  (non-atomic property is thread-unsafe. Comments are welcomed)

Thread-safe -> If any object not allow to modify by more than one thread at the same time.Immutable objects are generally thread-safe. (atomic property attribute type. Comments are  welcomed)

In general, immutable classes like NSArray, let are thread-safe, while their mutable variants like NSMutableArray,var are thread-unsafe.



What is @escaping and @nonescaping in swfit?

Ans : 

Closure :
Closure are self contained blocks of functionality that can be passed around and used in code.

In swift 1.x and 2.x, closure parameter is @escaping by default. It means closure can be escape during function body execution. If don't want to escape, we have to mention @nonescaping as parameter.

In swift 3.x and after, closure parameter is @nonescaping by default.

1. @nonescaping closure : When passing closure as function argument, the closure gets executed with function's body and returns the compiler back. As the execution ends, the passed closure goes out of scope and doesn't exist in memory.

Life cycle @nonescaping closure :

i. Pass closure as function argument, during the function call.
ii. Do some additional work with function
iii. Function runs the closure.
iv. Function returns the compiler back.

i.e

func getSumOf(array:[Int], handler: ((Int)->Void)) {
        //step 2
        var sum: Int = 0
        for value in array {
            sum += value
        }
     
        //step 3
        handler(sum)
    }
 
    func doSomething() {
        //setp 1
        self.getSumOf(array: [16,756,442,6,23]) { [weak self](sum) in
            print(sum)
            //step 4, finishing the execution
        }
    }

So here , here closure is executed at the end of function body executed.





2. @escping closure : When passing a closure as function argument, and after function body executed and closure need to execute later, and returns compiler back. As the execution ends, the scope of passed closure exist and have existence in memory, till the closure gets executed.

Asynchronous Execution: When you are executing the closure asynchronously on despatch queue, the queue will hold the closure in memory for you, to be used in future. In this case you have no idea when the closure will get executed.

Lifecycle of the @escaping closure:
1. Pass the closure as function argument, during the function call.
2. Do some additional work in function.
3. Function execute the closure asynchronously or stored. It stored either as a property or by being captured by another closure.
4. Function returns the compiler back.

Asynchronous Execution :

func getSumOf(array:[Int], handler: @escaping ((Int)->Void)) {
        //step 2
        var sum: Int = 0
        for value in array {
            sum += value
        }
        //step 3
        Globals.delay(0.3, closure: {
            handler(sum)
        })
    }
 
    func doSomething() {
        //setp 1
        self.getSumOf(array: [16,756,442,6,23]) { [weak self](sum) in
            print(sum)
            //step 4, finishing the execution
        }
    }
//Here we are calling the closure with the delay of 0.3 seconds
//It will print the sum-of all the passed numbers.

Always remember to use weak self while using a closure.