Search Your Question

Showing posts with label XIPHIAS Software Technologies. Show all posts
Showing posts with label XIPHIAS Software Technologies. Show all posts

What is retain cycle or strong cycle?

Ans : 

According to ARC, variable should be one of three Strong, Weak or unowned. By default, variable has strong reference.

Let's take example :

class Employee {
var name  : String
var emid  : String
var title : String
init(name: String,emid: String, title: String) {
           self.name = name
   self.emid = emid
   self.title = title
        }

        deinit {
print("Employee : \(name) removed")
}
}

var manan : Employee? = Employee(name: "Manan", emid: "1234", title: "Sr iOS Developer")
manan = nil

Here deinit will be called and output will be "Employee Manan removed"

Now we add reference :

var sagar = manan
manan = nil

Here deinit will not be called due to adding reference. Here sagar has strong reference to variable which owned by manan before.

Now,

var sagar = manan
manan = nil
sagar = nil

Here deinit will be called and again output will be "Employee Manan removed"


Now let's discuss about Strong cycle :

Strong cycle occurs when two object has strong reference to each other. i.e objectA has strong reference to objectB and objectB has strong reference to objectA.


example :

class Employee {
var name  : String
var emid  : String
var title : String
var macBook : MacBook?
init(name: String,emid: String, title: String) {
           self.name = name
   self.emid = emid
   self.title = title
        }

        deinit {
print("Employee : \(name) removed")
}
}

class MacBook {
var serialNumber: String
var assignee : Employee?
        init(serialNumber: String) {
              self.serialNumber = serialNumber
        }

deinit {
print("Macbook : \(serialNumber) removed")
}
}

var manan : Employee? = Employee(name: "Manan", emid: "1234", title: "Sr iOS Developer")
var mac : MacBook? = MacBook(serialNumber: "ABC123")
manan?macBook= mac
manan = nil
mac = nil

Here, output :

Employee Manan removed
Macbook ABC123 removed

Why? Because here there is no strong reference from both side. So retain cycle is not yet created.

Now,

var manan : Employee? = Employee(name: "Manan", emid: "1234", title: "Sr iOS Developer")
var mac : MacBook? = MacBook(serialNumber: "ABC123")
manan?macBook= mac
mac?.assignee = manan
manan = nil
mac = nil

Output will be :

Nothing will be removed. Not output comes. Because of retain cycle. So there are strong relationship between both objects like manan has mac and mac is assigned to manan.

So, if we want to solve this problem we have to use weak or unowned reference for one side.

So,

weak var assignee : Employee?

Now output will be :

Employee Manan removed
Macbook ABC123 removed

So Now unowned : It is same as weak as it does not hold strong relationship with object. So when to use unowned : An unowned reference is used when the other instance has the same lifetime or a longer lifetime.

Difference between weak and unowned :


  • weak reference is used where there is possibility for that reference to become nil at some point during its lifetime. 
  • An unowned reference is used where there is no possibility for that reference becoming nil at any point until the self-object exist.
Example :

Every employee may or may not hold ICard but every ICard must be assigned to some one.
So Employee class has ICard variable as optional, but in ICard class, employee variable with optional and weak is not possible. If we want to use weak, we should make employee variable in ICard class optional. But we don't want that.

So, solution is that : 

unowned var employee : Employee

Now we are cleared about strong, weak and unowned and also our question : Retain Cycle or Strong Cycle.

Credit : Vinod Shwami

Application States

Ans : 

Before iOS 4.0 there were 3 states. Not running, inactive and active.

  1. Non-running - The app is not running.
  2. Inactive - The app is running in the foreground, but not receiving events. An iOS app can be placed into an inactive state, for example, when a call or SMS message is received.
  3. Active - The app is running in the foreground, and receiving events.
  4. Background - The app is running in the background, and executing code.
  5. Suspended - The app is in the background, but no code is being executed.
For maintaining above states, there are following app delegate methods:


application:willFinishLaunchingWithOptions (First method)
Method called when the launch process is initiated. This is the first opportunity to execute any code within the app.

application:didFinishLaunchingWithOptions
Method called when the launch process is nearly complete. Since this method is called is before any of the app's windows are displayed, it is the last opportunity to prepare the interface and make any final adjustments.

applicationDidBecomeActive
Once the application has become active, the application delegate will receive a callback notification message via the method applicationDidBecomeActive.

This method is also called each time the app returns to an active state from a previous switch to inactive from a resulting phone call or SMS.

applicationWillResignActive
 Each time a temporary event, such as a phone call, happens this method gets called. It is also important to note that "quitting" an iOS app does not terminate the processes, but rather moves the app to the background.

applicationDidEnterBackground
This method is called when an iOS app is running, but no longer in the foreground. In other words, the user interface is not currently being displayed. According to Apple's UIApplicationDelegate Protocol Reference, the app has approximately five seconds to perform tasks and return. If the method does not return within five seconds, the application is terminated.

applicationWillEnterForeground
This method is called as an app is preparing to move from the background to the foreground. The app, however, is not moved into an active state without the applicationDidBecomeActive method being called. This method gives a developer the opportunity to re-establish the settings of the previous running state before the app becomes active.

applicationWillTerminate
This method notifies your application delegate when a termination event has been triggered. Hitting the home button no longer quits the application. Force quitting the iOS app, or shutting down the device triggers the applicationWillTerminate method. This is the opportunity to save the application configuration, settings, and user preferences.



Difference between the atomic and nonatomic attributes?

Ans : 

Atomic property give guarantee that valid value will be return. But valid does not mean that correct value.

This also not mean that atomic property are thread safe. Different threads can attempt to read and write value at same time. So one of two value will be return - value before change or changed value.

So atomic property is suffering from performance hit due to locking and unlocking before and after get or set value.

Non atomic property has no guarantee regarding correct value, a partially correct value or may be garbage value.
This is not thread safe this enhanced speed of access property.

Atomic property lock while setting value,

While non atomic property does not lock while setting value. 

ARC - Automatic Reference Counting

Ans : Automatic Reference Counting is memory management feature in iOS that provides automatic referencing counting system. According to attribute type of property like retain and release, it increment and decrements reference count at runtime.

ARC is does not handle reference cycle automatically. 

Unlike garbage collection, ARC does not handle reference cycles automatically.


Default property attributes : 

i> Memory management : strong  weak  copy  assign 
ii> Thread Safety : atomic nonatomic
iii> Mutability : readwrite readonly

@property (strong, atomic, readwrite) NSArray *name;

For IBOutlet,

@property (nonatomic, retain) IBOutlet UILabel *label;


@property (weak) IBOutlet UILabel *instructions;
In 2015, apple recommend to use Strong.


To stop retain cycle, user should mention weak reference when needed.

Q : What is retain?
A.Retain works same as Strong according to apple document. If we assign retain, it will convert to strong or consider as Strong. 

Read : Difference between Strong and Weak attribute

What is protocol? Why it is used?

Ans : Protocols are way to specify a set of methods that class has to implement if class want to work with this protocol. Protocol has 2 types of methods like Required type and Optional type methods.

1. If @required are tagged above methods, then these methods must be implemented on class which confirm this protocol.

2. If @optional are tagged above methods, then these methods is not necessary to implement on it.

Why it is used? 

Protocol provide blueprint methods for classes. It has certain methods and parameters that classes have to implement in its body. Its like that classes should have certain characteristic if it confirm protocol. Like if you want to become human(class) you must be sleep, eat, breathe(characteristics).

For more detail,
I have created Delegate and Protocol. See example.

Difference between Delegate and Datasource

Ans :

A delegate type object responds to actions that another object takes.
i.e  the UITableViewDelegate protocol has methods such as didSelectRowAtIndexPath for performing actions upon a user selecting a particular row in a table and willDisplayCell which called before delegate use cell to draw row.

DataSource type object gives data to another object.
i.e UITableViewDataSource protocol has methods such as cellForRowAtIndexPath and numberOfSectionInTaboeView dictating what should be displayed in the table.

Understanding Delegate in More Detail : 

If Object X call Object Y to perform an action. Object X should know when Object Y complete task and take action after that. 

Here we can tell that X is delegate object of Y. Y will have a reference of X. So X will implement delegate methods of Y. So Y can notify to X via delegate method.

One more point we can say that Delegate about controlling of UI and DataSource about controlling data.

Buy CHRYOSOLITE Superman Fan Art Men's Round Neck TShirt