Search Your Question
What is Protocol Oriented Programming?
Difference between compact map and Flat map
Compact Map :
Use this method to receive an array of nonoptional values when your transformation produces an optional value.
let scores = ["1", "2", "three", "four", "5"]
let mapped: [Int?] = scores.map { str in Int(str) }
// [1, 2, nil, nil, 5] - Two nil values as "three" and "four" are strings.
let compactMapped: [Int] = scores.compactMap { str in Int(str) }
// [1, 2, 5] - The nil values for "three" and "four" are filtered out.
Flat Map :
let scoresByName = ["Henk": [0, 5, 8], "John": [2, 5, 8]]
let mapped = scoresByName.map { $0.value }
// [[0, 5, 8], [2, 5, 8]] - An array of arrays
print(mapped)
let flatMapped = scoresByName.flatMap { $0.value }
// [0, 5, 8, 2, 5, 8] - flattened to only one array
How to switch from background queue to main queue?
If you're on a background thread and want to execute code on the main thread, you need to call async() again. However, this time, you do it on DispatchQueue.main, which is the main thread, rather than one of the global quality of service queues.
DispatchQueue.global(qos: .userInitiated).async {
if let url = URL(string: urlString) {
if let data = try? Data(contentsOf: url) {
self.parse(json: data)
return
}
}
self.showError()
}
func showError() {
DispatchQueue.main.async {
let ac = UIAlertController(title: "Loading error", message: "There was a problem loading the feed; please check your connection and try again.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
self.present(ac, animated: true)
}
}
Sof here, If I want to work with UI-related stuff, I must switch queue to main. So to execute that code under DispatchQueue.main.async { } block.
Difference between FCM and APNS
- FCM is sent as JSON payloads and APNS sends either string or dictionary.
- FCM has a payload of 2KB while APNS has a payload of 4KB.
- APNS saves 1 notification per App while FCM saves 100 notifications per device.
- FCM supports multiple platforms while APNS requires their proprietary platform.
- Acknowledgment can be sent in FCM if using XMPP, but it's not possible on APNS.
Advatage of FCM
- Even if the user disallows notification, you can notify your app if the app is running in the foreground (using shouldEstablishDirectChannel).
- Don't need to create dashboard to send notification on the device.
- Notification analytics on FCM Dashboard.
- Easy to create notification payload structure.
- App Server side handling is easy, Only one key is required for multiple apps and platform (iOS, Android, Web)
GIT Interview Questions 2
- GIT Commit command
The command that is used to write a commit message is “git commit -a”
Now explain about -a flag by saying -a on the command line instructs git to commit the new content of all tracked files that have been modified. Also, mention you can use “git add <file>” before git commit -a if new files need to be committed for the first time. - How can you fix a broken commit?
In order to fix any broken commit, use the command “git commit --amend”. When you run this command, you can fix the broken commit message in the editor. Basic Git commands:
Command Function git rm [file]
deletes the file from your working directory and stages the deletion. git log
list the version history for the current branch. git show [commit]
shows the metadata and content changes of the specified commit. git tag [commitID]
used to give tags to the specified commit. git checkout [branch name]
git checkout -b [branch name]
used to switch from one branch to another. creates a new branch and also switches to it.
- In Git how do you revert a commit that has already been pushed and made public?
There can be two approaches to tackle this question and make sure that you include both because any of the below options can be used depending on the situation:
Remove or fix the bad file in a new commit and then push it to the remote repository. This is the most obvious way to fix an error. Once you have made necessary changes to the file, then commit it to the remote repository using the command: git commit -m “commit message”
Also, you can create a new commit that undoes all changes that were made in the bad commit. To do this use the command
git revert <name of bad commit> - What is the difference between git pull and git fetch?
Git pull command pulls new changes or commits from a particular branch from your central repository and updates your target branch in your local repository.
Git fetch is also used for the same purpose but it works in a slightly different way. When you perform a git fetch, it pulls all new commits from the desired branch and stores it in a new branch in your local repository. If you want to reflect these changes in your target branch, git fetch must be followed with a git merge. Your target branch will only be updated after merging the target branch and fetched branch. Just to make it easy for you, remember the equation below:
Git pull = git fetch + git merge - What is git stash?
Often, when you’ve been working on part of your project, things are in a messy state and you want to switch branches for some time to work on something else. The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later. The answer to this issue is Git stash.
Stashing takes your working directory that is, your modified tracked files and staged changes and saves it on a stack of unfinished changes that you can reapply at any time. - What is the function of ‘git stash apply’?
If you want to continue working where you had left your work then ‘git stash apply‘ command is used to bring back the saved changes onto your current working directory. - What is git stash drop?
Git ‘stash drop’ command is used to remove the stashed item. It will remove the last added stash item by default, and it can also remove a specific item if you include it as an argument.
Now give an example.
If you want to remove a particular stash item from the list of stashed items you can use the below commands:
- git stash list: It will display the list of stashed items like:
- stash@{0}: WIP on master: 049d078 added the index file
- stash@{1}: WIP on master: c264051 Revert “added file_size”
- stash@{2}: WIP on master: 21d80a5 added number to log
If you want to remove an item named stash@{0} use command git stash drop stash@{0}.
- Can you explain the Gitflow workflow?
To record the history of the project, Gitflow workflow employs two parallel long-running branches – master and develop:
Master – this branch is always ready to be released on LIVE, with everything fully tested and approved (production-ready).
Hotfix – these branches are used to quickly patch production releases. These branches are a lot like release branches and feature branches except they’re based on master instead of develop.
Develop – this is the branch to which all feature branches are merged and where all tests are performed. Only when everything’s been thoroughly checked and fixed it can be merged to the master.
Feature – each new feature should reside in its own branch, which can be pushed to the develop branch as their parent one. - What is Git fork? What is the difference between fork, branch, and clone?
A fork is a copy of a repository. Normally you fork a repository so that you are able to freely experiment with changes without affecting the original project. Most commonly, forks are used to either propose changes to someone else’s project or to use someone else’s project as a starting point for your own idea.
git cloning means pointing to an existing repository and make a copy of that repository in a new directory, at some other location. The original repository can be located on the local file system or on remote machine accessible supported protocols. The git clone command is used to create a copy of an existing Git repository.
In very simple words, git branches are individual projects within a git repository. Different branches within a repository can have completely different files and folders, or it could have everything the same except for some lines of code in a file. - What is the difference between rebasing and merge in Git?
In Git, the rebase command is used to integrate changes from one branch into another. It is an alternative to the “merge” command. The difference between rebasing and merge is that rebase rewrites the commit history in order to produce a straight, linear succession of commits.
Merging is Git’s way of putting a forked history back together again. The git merge command helps you take the independent lines of development created by git branch and integrate them into a single branch. - What is git cherry-pick?
The command git cherry-pick is normally used to introduce particular commits from one branch within a repository onto a different branch. Another common use is to forward- or back-port commits from a maintenance branch to a development branch. This is in contrast with other ways such as merge and rebase which normally apply many commits onto another branch.
Consider:
git cherry-pick <commit-hash> - How do you squash the last N commits into a single commit?
There are two options to squash the last N commits into a single commit include both of the below-mentioned options in your answer
If you want to write the new commit message from scratch use the following command
git reset –soft HEAD~N &&git commit
If you want to start editing the new commit message with a concatenation of the existing commit messages then you need to extract those messages and pass them to Git commit for that I will use
git reset –soft HEAD~N &&git commit –edit -m”$(git log –format=%B –reverse .HEAD@{N})” - How do I rename a local Git branch?
Here are the steps to rename the branch:
Switch to the branch which needs to be renamed
git branch -m <new_name>
git push origin :<old_name>
git push origin <new_name>:refs/heads/<new_name>
What is MVVM?
Download task when app is inactive
For the background download, there is beginBackgroundTaskWithExpirationHandler:
that is specifically designed to do that. When you use it, you will get few more minutes to execute whatever you need (after that limit, your application will get terminated no matter what).
You can write following methods:
func beginBackgroundTask() -> UIBackgroundTaskIdentifier {
return UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({})
}
func endBackgroundTask(taskID: UIBackgroundTaskIdentifier) {
UIApplication.sharedApplication().endBackgroundTask(taskID)
}
When you want to use it, you just simple begin / end the task when starting / finishing the download call:
// Start task
let task = self.beginBackgroundTask()
// Do whatever you need
self.someBackgroundTask()
// End task
self.endBackgroundTask(task)
Hope it helps!
What is SSL Pinning and How to implement it?
SSL stands for Secure Socket Layer, which is a protocol for creating an encrypted connection between client and server. It ensures that all data pass in network will be private and integral.
How SSL works? When client establishes the connection with server (called SSL handshake):
- Client connects to server and requests server identify itself.
- Server sends certificate to client (include public key)
- Client checks if that certificate is valid. If it is, client creates a symmetric key (session key), encrypts with public key, then sends back to server
- Server receives encrypted symmetric key, decrypts by its private key, then sends acknowledge packet to client
- Client receives ACK and starts the session
- Maintain the old certificate for a time, until we make sure all users have downloaded new version already.
Explain NSOPERATION Priority.
All operations may not be equally important. Setting the queuePriority property will promote or defer an operation in an NSOperationQueue according to the following rankings: