Wednesday, July 25, 2018

How to copy Xcode from one Mac to another Mac

Move Xcode from one Mac to another Mac 

Steps 

1- Copy Xcode.app file and paste to new mac's `Application` folder
2- Copy `~/Library/Developer/` to new mac's location ~/Library/Developer/ folder
3-

Issue -
Xcode won't start, stuck on 'Verifying “Xcode”…'

Solution 
xattr -d com.apple.quarantine '/Applications/Xcode.app'

By running above command , you are giving persission to Xcode ti install it without verifying it. 

 4- open Xcode in Application directory. 

Cheers !!!!

Android Architecture Component ROOM

Why ROOM Library ?

In android app, We convert raw data into structural data . we use SQLite core library to achieve it .

 It handles CRUD (Create, Read, Update and Delete) operations required for a database. Java classes and interfaces for SQLite are provided by the android.database. SQLite maintains an effective database management system. But this conventional method has its own disadvantages.


  • You have to write long repetitive code, which will be time-consuming as well as prone to mistakes.
  • Syntax errors in queries
  • No compile time error detection (Time consuming)
  • Parsing is required to convert data to Plain Old Java Objects (POJO) objects
  • It is very difficult to manage SQL queries for a complex relational database.
To overcome this, Google has introduced Room Persistence Library.


What is ROOM? 

Room provides the abstraction layer over the SQLite DB. ROOM is not a database
It has extended features and ease of use over android SQLite library .

Componentes

There are three main components of Room-
  • Database: Contains the database holder and serves as the main access point for the underlying connection to your app's persisted, relational data.
    The class that's annotated with @Database should satisfy the following conditions:
    • Be an abstract class that extends RoomDatabase.
    • Include the list of entities associated with the database within the annotation.
    • Contain an abstract method that has 0 arguments and returns the class that is annotated with @Dao.
    At runtime, you can acquire an instance of Database by calling Room.databaseBuilder() orRoom.inMemoryDatabaseBuilder().
  • Entity: Represents a table within the database.   
  1. tableName attribute is used to define the name of the table
  2. Every entity class must have at-least one Primary Key field, annotated with @PrimaryKey
  3. Fields in entity class can be annotated with @ColumnInfo(name = “name_of_column”) annotation to give specific column names
         
  • DAO: Contains the methods used for accessing the database.

  1. Data Access Object is either be an interface or an abstract class annotated with @Doa annotation, containing all the methods to define the operations to be performed on data. The methods can be annotated with
    • @Query to retrieve data from database
    • @Insert to insert data into database
    • @Delete to delete data from database
    • @Update to update data in database



                                 The app uses the Room database to get the data access
  objects, or DAOs, associated with that database. The app then uses each DAO to
  get entities from the database and save any changes to those entities back to
  the database. Finally, the app uses an entity to get and set values that
  correspond to table columns within the database.




How to Implement?

Before jump into details, Room uses annotations heavily to decrease the boilerplate code.






Why and What is ViewModel : Android Architecture Component

Why We need model ?


A ViewModel holds your app's UI data in a lifecycle-conscious way that survives configuration changes. Separating your app's UI data from your Activity and Fragment classes lets you better follow the single responsibility principle: Your activities and fragments are responsible for drawing data to the screen, while your ViewModel can take care of holding and processing all the data needed for the UI.

In the ViewModel, use LiveData for changeable data that the UI will use or display. Using LiveData has several benefits:

You can put an observer on the data (instead of polling for changes) and only update the
the UI when the data actually changes.

The Repository and the UI are completely separated by the ViewModel. There are no database calls from the ViewModel, making the code more testable.


What is ViewModel ?

The ViewModel's role is to provide data to the UI and survive configuration changes. A ViewModel acts as a communication center between the Repository and the UI. You can also use a ViewModel to share data between fragments. The ViewModel is part of the lifecycle library.






Warning: Never pass context into ViewModel instances. Do not store Activity, Fragment, or View instances or their Context in the ViewModel.


For example, an Activity can be destroyed and created many times during the lifecycle of a ViewModel as the device is rotated. If you store a reference to the Activity in the ViewModel, you end up with references that point to the destroyed Activity. This is a memory leak.


If you need the application context, use AndroidViewModel


Important: ViewModel is not a replacement for the onSaveInstanceState() method, because the ViewModel does not survive a process shutdown. 


Friday, June 29, 2018

Why and What is DiffUtil in android

Why DiffUtil / Problem with notifyDatasetChanged ?

When we use notifyDataSetChanged() method it updates all the view (all visible view on screen and few buffer view above and below the screen). It’s inappropriate way of updating list if just a few rows have changed. For example, your friend changes the status on WhatsApp then only that row should be updated instead of all the rows.


What is DiffUtil 

Android has provided a class called DiffUtil under version 7 support utils. As per android documentation, the definition of DiffUtil is as follow

DiffUtil is a utility class that can calculate the difference between two lists and output a list of update operations that converts the first list into the second one.


Performance 


The algorithm is optimized for space. It requires O(N) space to find out the number of operations for transforming the old list into the new one. It’s expected performance is O(N + D²). N is the sum of the lengths of the two lists and D is the length of the edit script. The edit script is the smallest set of deletions and insertions to transform the first list into the second.  


How to use?


DiffUtil.Callback is an abstract class and used as callback class by DiffUtil while calculating the difference between two lists. It has four abstract methods and one non-abstract method. You have to extend it and override all its methods-

Callback is an abstract class and needs to override methods about the sizes of the two lists and the comparison for items at particular index. I’m not familiar with the internal implementation, I had to add null checks (and left out some) just for sure. Of course, lists can be of any type, generifying the Callback will only deepens the comparisons of the items.

You may notice the getChangePayload() method, which is not abstract. This method is called when the areItemsTheSame() returns true, but areContentsTheSame() returns false, which means that we are talking about the same item but the fields data might have changed. Basically, this method returns the reason(s) why there is a difference in the lists.

In this, I used a Bundle to return more than one reason about the changes which make the compared items content not the same.

Once we have the callback, it is pretty easy.
@ Override
public void onNewProducts(List<Product> newProducts) {
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new ProductListDiffCallback(mProducts, newProducts));
    diffResult.dispatchUpdatesTo(mProductAdapter);

}

getOldListSize()– Return the size of the old list.

getNewListSize()– Return the size of the new list.

areItemsTheSame(int oldItemPosition, int newItemPosition)– It decides whether two objects are representing same items or not.

areContentsTheSame(int oldItemPosition, int newItemPosition)– It decides whether two items have same data or not. This method is called by DiffUtil only if areItemsTheSame() returns true.

getChangePayload(int oldItemPosition, int newItemPosition)– If areItemTheSame() returns true and areContentsTheSame() returns false than DiffUtil utility calls this method to get a payload about the change.

DiffUtil also uses methods of RecyclerView.Adapter to notify the Adapter to update the data set:

notifyItemMoved()
notifyItemRangeChanged()
notifyItemRangeInserted()
notifyItemRangeRemoved()

You can read more details on RecyclerView.Adapter and its method here.



Important:


If the lists are large, this operation may take significant time so you are advised to run this on a background thread, get the DiffUtil#DiffResult then apply it on the RecyclerView on the main thread. Also max size of the list can be 2²⁶ due to implementation constraints.


Github Sample -> https://github.com/manishpathak99/DiffUtil-sample




Wednesday, April 25, 2018

How to get Bundle Identifier of an iOS App from Apple AppStore/ IPA file

Find the Bundle Identifier of An iOS Application (App Bundle ID OR Bundle ID)


If your app is in the App Store.

1- Find the app online (Google for the iTunes link). For this example we use Apple Pages: https://itunes.apple.com/app/pages/id361309726?mt=8.

2- Copy the number after the id in the URL. (Here: 361309726).

3- Open https://itunes.apple.com/lookup?id=361309726 where you replace the ID with the one you looked up.

4- Search the output for "bundleID". In this example it looks like this (next to a bunch of other data): "bundleId":"com.apple.Pages". So for Apple, the bundle ID is com.apple.Pages.

If you have the .ipa file directly

1- Copy the .ipa file and rename the extension to .zip. (So e.g. MyApp.ipa will become MyApp.zip)

2- Unzip the zip file. You will get a new folder named like the zip file.

3- Search for the file iTunesMetadata.plist in that new folder.

4- Open the file with a text editor and search for softwareVersionBundleId. For Pages app this looks like this and is com.apple.Pages:


Friday, January 19, 2018

Why Machine Learning , what is Machine Learning and how Machine Learning works?

What is Machine Learning  ? 

Machine learning is a method of data analysis that automates analytical model building. It is a branch of artificial intelligence based on the idea that machines should be able to learn and adapt through experience. 

Think of machine learning like this. As a human, and as a user of technology, you complete certain tasks that require you to make a decision or classify something. For instance, when you read your inbox in the morning, you decide to mark that ‘Win a Free Cruise if You Click Here’ email as spam. How would a computer know to do the same thing?

Machine learning is comprised of algorithms that teach computers to perform tasks that human beings do naturally on a daily basis.

Machine learning algorithms teach computers to recognize features of an object.

For example, a computer is shown an apple and told that it is an apple. The computer then uses that information to classify the various characteristics of an apple, building upon new information each time. At first, a computer might classify an apple as round, and build a model that states that if something is round, it’s an apple. Then later, when an orange is introduced, the computer learns that if something is round AND red, it’s an apple. Then a tomato is introduced, and so on and so forth. The computer must continually modify its model based on new information and assign a predictive value to each model, indicating the degree of confidence that an object is one thing over another. For example, yellow is a more predictive value for a banana than red is for an apple.


1- Self-driving Google car
2- Online recommendation offers such as those from Amazon and Netflix
3- Fraud detection
4- IVR customer care 


WHY Machine Learning?

Machine learning today is not like machine learning of the past. It was born from pattern recognition and the theory that computers can learn without being programmed to perform specific tasks; researchers interested in artificial intelligence wanted to see if computers could learn from data. The iterative aspect of machine learning is important because as models are exposed to new data, they are able to independently adapt. They learn from previous computations to produce reliable, repeatable decisions and results.


How It Works?






Popular machine learning methods?

wo popular methods of machine learning are supervised learning and unsupervised learning. It is estimated that about 70 percent of machine learning is supervised learning, while unsupervised learning ranges from 10 – 20 percent. 

Supervised Learning

This kind of learning is possible when inputs and the outputs are clearly identified, and algorithms are trained using labeled examples. To understand this better, let’s consider the following example: an equipment could have data points labeled F (failed) or R (runs).
This kind of learning is possible when inputs and the outputs are clearly identified, and algorithms are trained using labeled examples.




The learning algorithm using supervised learning would receive a set of inputs along with the corresponding correct output to find errors. Based on these inputs, it would further modify the model accordingly. This is a form of pattern recognition, as supervised learning happens through methods like classification, regression, prediction, and gradient boosting. Supervised learning uses patterns to predict the values of the label on additional unlabeled data.

for example such as fraudulent credit card transactions.


Unsupervised Learning

Unsupervised learning is used with data sets without historical data. An unsupervised learning algorithm explores surpassed data to find the structure. This kind of learning works best for transactional data;



Popular techniques where unsupervised learning is used also include self-organizing maps, nearest neighbor mappig, singular value decomposition, and k-means clustering. Basically, online recommendations, identification of data outliers, and segment text topics are all examples of unsupervised learning.



Semi-Supervised Learning


Semi-supervised learning is a bit of both supervised and unsupervised learning and uses both labeled and unlabeled data for training. 



This type of learning can again be used with methods such as classification, regression, and prediction. Examples of semi-supervised learning would be face and voice recognition techniques.


Reinforcement Learning

This is a bit similar to the traditional type of data analysis; the algorithm discovers through trial and error and decides which action results in greater rewards. Three major components can be identified in reinforcement learning functionality: the agent, the environment, and the actions. The agent is the learner or decision-maker, the environment includes everything that the agent interacts with, and the actions are what the agent can do.






Machine Learning Algorithms And Processes


you should familiarize yourself with these common machine learning algorithms and processes: neural networks, decision trees, random forests, associations and sequence discovery, gradient boosting and bagging, support vector machines, self-organizing maps, k-means clustering, Bayesian networks, Gaussian mixture models, and more.
Other tools and processes that pair up with the best algorithms to aid in deriving the most value from big data include:

  • Comprehensive data quality and management
  • GUIs for building models and process flows
  • Interactive data exploration and visualization of model results
  • Comparisons of different machine learning models to quickly identify the best one
  • Automated ensemble model evaluation to identify the best performers
  • Easy model deployment so you can get repeatable, reliable results quickly