Search Your Question

What is Generics in Swift? Write a program to make and use generics.

Ans :

Apple doc says: Generic code enables you to write flexible, reusable functions and types that can work with any type. You can write code that avoids duplication by using generics in most cases.

Code for Example :


 let intArray = [1,2,3,4,5] // type int
 let stringArray = [“abhi”, “iOS”] // type string



If we want to print all values, we have to make 2 functions accepting integer array or string array.
But using Generic, we can make 1 method accepting generic type.



 func printAnyArray<T>(arr:[T]) {
       arr.map { print($0) }

 }


 <T> after the function name represents a generic function. A generic function can work with whatever type. We can put whatever instead of T. T is placeholder type. We can use for dictionary like Dictionary<key,value> and for array like Array<element> .

Generic function with parameter type : 


//Declare
func anotherMethod<T, U> (first: T, second: U) { print("first one called")}
func anotherMethod<T> (first: T, second: T) { print("second one called")}
//Call
anotherMethod(first: 123, second: "456"// T - Int, U - String
anotherMethod(first: 123, second: 456// T - Int




In above methods, we use different placeholder type in method1 and same placeholder type in method2. If out passing parameter types are different then 2nd method will be called, and if same then 1st method will be called.

Generic function with type constraints :


func middleVal<T>(array: [T]) -> T? {
  guard !array.isEmpty else { return nil }
  return array.sorted()[(array.count - 1) / 2]
}

   

Sometimes, we require specific types only i.e type which confirm comparable protocol


func middleVal<T: Comparable>(array: [T]) -> T? {
  guard !array.isEmpty else { return nil }
  return array.sorted()[(array.count - 1) / 2]
}

    

Above method will only accepts. int, float, double.


If you have any comment, question, or recommendation, feel free to post them in the comment section below!  

What is callback function?

Ans : A callback function is a function that is passed as an argument to another function, to be “called back” at a later time.

Code for Example :


func printValue(index: Int, element: Int) {
    print("index = \(index), Element = \(element)")
}
func each(array: [Int], callback: (Int, Int) -> Void) {
    for i in 0..<array.count {
        callback(i, array[i])
    }
}

each(array: [1, 2, 3, 4, 5],callback: printValue)


Here each function type is ( [Int], (Int, Int) -> Void ) .
Here callback function type is ( (Int, Int) -> Void ) .

Callback function mostly used when we want to pass block of code that should be executed in another function execution complete.

What are best approach to implement if large data is coming from core data and show data on tableview?

Ans : 

Use NSFetchResultsController : A controller that you use to manage the results of a Core Data fetch request and to display data to the user.


If we have very large data in database and we want all data to show in tableview, then we have to write some extra code on core data fetch request as fetching data will stuck UI.
Then we can use NSFetchResultsController as it has ability to fetch data from memory in batches. So data coming in batch when we scroll tableview and saved in cache.

We have to make property of type NSFetchResultsController :

- (NSFetchedResultsController *)fetchedResultsController {
    
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
        entityForName:@"FailedBankInfo" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    
    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
        initWithKey:@"details.closeDate" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
    
    [fetchRequest setFetchBatchSize:20];
    
    NSFetchedResultsController *theFetchedResultsController =
        [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
            managedObjectContext:managedObjectContext sectionNameKeyPath:nil
            cacheName:@"Root"];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;
    
    return _fetchedResultsController;
    
}


and in ViewDidLoad :

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSError *error;
 if (![[self fetchedResultsController] performFetch:&error]) {
  // Update to handle the error appropriately.
  NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  exit(-1);  // Fail
 }
    
    self.title = @"Failed Banks";
    
}


We get data in numberOfRowInSection :
id  sectionInfo =
        [[_fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];

We get data in cellForRowAtIndexPath :
FailedBankInfo *info = [_fetchedResultsController objectAtIndexPath:indexPath];

We also have to write FetchResultsControllerDelegate methods as it is as at apple provide at end of ViewController's code.

Enjoy lazy loading data...

Tutorial : Ray Wanderlich


How you can add column in entity in core data that can be also added in next release?

Ans : When you initialise a Core Data stack, one of the steps involved is adding a store to the persistent store coordinator. When you encounter this step, Core Data does a few things prior to adding the store to the coordinator. First, Core Data analyzes the store’s model version. Next, it compares this version to the coordinator’s configured data model. If the store’s model version and the coordinator’s model version don’t match, Core Data will perform a migration, when enabled.

Note: If migrations aren’t enabled, and the store is incompatible with the model, Core Data will simply not attach the store to the coordinator and specify an error with an appropriate reason code.

Migrations happen in three steps:
  1. Core Data copies over all the objects from one data store to the next. 
  2. Core Data connects and relates all the objects according to the relationship mapping. 
  3. Enforce any data validations in the destination model. Core Data disables destination model validations during the data copy.
Database Migration : 

Lightweight Migration
Manual Migration
Custom Manual Migration
Fully Manual Migration

Setting for Lightweight Migration: 

NSMigratePersistentStoresAutomaticallyOption: true
NSInferMappingModelAutomaticallyOption: true


Coredata Migration Tutorial : RayWanderLich


What is Core Data Stacks?

Ans : Core Data is Apple’s object graph management and persistency framework.

Core Data Stack
Core Data Stack


  1. Managed Object
  2. Managed Object Context
  3. Managed Object Model
  4. Persistent Store Coordinator

  1. NSManagedObject’s are the model objects exposed by Core Data.
  2. NSManagedObjectModel is a database schema that describes the application’s entities. It defines the structure of the managed objects.
  3. NSPersistentStoreCoordinator associates persistent storage and managed object model. It lends itself to mapping the data from the storage, say SQLite data base rows, into the object model. It is a task of high complexity and is often taken for granted when working with Core Data.What is more, the persistent coordinator is used by the managed object context when it comes to saving or fetching objects.
  4. NSManagedObjectContext controls the life cycle of the managed objects and provides operations to create, save and fetch them. You can think of the managed object context as a scratch pad with a group of managed objects.

Starting from iOS 10, NSPersistentContainer is responsible for creation and management of the Core Data stack.
NSPersistentContainer exposes a managed object model, a managed object context and a persistent store coordinator as well as provides many convenience methods when working them, especially when it comes to multithreaded applications.