|
| 1 | +### iOS Foundation |
| 2 | +1. **Explain the iOS app lifecycle.** |
| 3 | +The iOS app lifecycle defines the sequence of states an app goes through from launch until termination. Understanding these states is essential because it helps developers manage resources and handle transitions appropriately, leading to a smoother user experience. Here’s an overview of each key state: |
1 | 4 |
|
| 5 | +### 1. **Not Running** |
| 6 | + - The app is in this state before it’s launched or after it has been terminated. |
| 7 | + - There is no code being executed, and the app’s resources are completely freed. |
2 | 8 |
|
| 9 | +### 2. **Inactive** |
| 10 | + - The app enters the inactive state briefly when it is transitioning between states, such as moving to the foreground or background. |
| 11 | + - For example, when a call is received, the app might go inactive temporarily. |
| 12 | + - In this state, the app is still visible but not actively receiving user input or events. |
| 13 | + - **Method**: `applicationWillResignActive(_:)` is called. |
| 14 | + |
| 15 | +### 3. **Active** |
| 16 | + - The app is fully in the foreground, receiving events, and interacting with the user. |
| 17 | + - This is typically the primary state where most of the app’s functionality occurs. |
| 18 | + - **Method**: `applicationDidBecomeActive(_:)` is called when the app transitions to this state. |
| 19 | + |
| 20 | +### 4. **Background** |
| 21 | + - The app moves to the background when the user presses the home button, switches to another app, or closes the app. |
| 22 | + - In this state, the app is no longer visible to the user but can continue to execute code for a short time. |
| 23 | + - **Tasks**: Apps have approximately 5 seconds to complete tasks before being suspended, although apps with specific needs (like navigation or music) can request additional background time. |
| 24 | + - **Method**: `applicationDidEnterBackground(_:)` is called when the app moves to the background. |
| 25 | + |
| 26 | +### 5. **Suspended** |
| 27 | + - After background execution completes, the app enters the suspended state where it remains in memory but doesn’t execute any code. |
| 28 | + - The app is frozen, meaning it retains its state, but no code runs until it’s reopened or brought to the foreground. |
| 29 | + - If the system needs more memory, suspended apps may be terminated without additional notifications to the app. |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +### Key Lifecycle Methods in `AppDelegate` |
| 34 | + |
| 35 | +1. **application(_:didFinishLaunchingWithOptions:)** |
| 36 | + - Called when the app has finished launching. Use this method to initialize settings, resources, or services. |
| 37 | + |
| 38 | +2. **applicationWillEnterForeground(_:)** |
| 39 | + - Called when the app is moving from the background to the active state. |
| 40 | + |
| 41 | +3. **applicationDidBecomeActive(_:)** |
| 42 | + - Called when the app has moved to the active state and can begin processing events. |
| 43 | + |
| 44 | +4. **applicationWillResignActive(_:)** |
| 45 | + - Called when the app is about to move from active to inactive, such as during an interruption. |
| 46 | + |
| 47 | +5. **applicationDidEnterBackground(_:)** |
| 48 | + - Called when the app has moved to the background. Use it to save data, release shared resources, and store enough state information to restore the app. |
| 49 | + |
| 50 | +6. **applicationWillTerminate(_:)** |
| 51 | + - Called when the app is about to be terminated. Save any critical data and clean up resources in this method. |
| 52 | + |
| 53 | +### Summary Diagram of iOS App Lifecycle |
| 54 | + |
| 55 | +```plaintext |
| 56 | + +-------------+ |
| 57 | + Launch | Not Running | |
| 58 | + +-------------+ |
| 59 | + | |
| 60 | + V |
| 61 | + +--------Inactive---------+ |
| 62 | + | | |
| 63 | + Foreground Background |
| 64 | + Transition Transition |
| 65 | + | | |
| 66 | + V V |
| 67 | + +--------+ User Interaction +--------+ |
| 68 | + | Active |<------------------>|Background| |
| 69 | + +--------+ +--------+ |
| 70 | + | | |
| 71 | + V V |
| 72 | + Inactive--------------------------Suspended |
| 73 | +``` |
| 74 | + |
| 75 | +Each of these states and transitions helps manage resources effectively and gives developers control over what happens during state changes, leading to smoother app performance. |
| 76 | + |
| 77 | +2. **What is the difference between `viewDidLoad`, `viewWillAppear`, and `viewDidAppear` in view controllers?** |
| 78 | +### Summary of Differences |
| 79 | +| Method | Called When | Purpose | Frequency | |
| 80 | +|------------------|------------------------------------------------|----------------------------------------------|-------------------------| |
| 81 | +| **viewDidLoad** | Once, after the view is loaded into memory | Initial setup and one-time configurations | Only once per lifecycle | |
| 82 | +| **viewWillAppear** | Every time before the view appears on screen | Refreshing/updating data or UI elements | Every appearance | |
| 83 | +| **viewDidAppear** | Right after the view has appeared on screen | Start animations, track analytics, etc. | Every appearance | |
| 84 | + |
| 85 | +4. **How does `UIViewController` differ from `UIView`?** |
| 86 | + |
| 87 | +Certainly, here’s a concise comparison: |
| 88 | + |
| 89 | +| Feature | `UIViewController` | `UIView` | |
| 90 | +|----------------------------|--------------------------------------------------------|-----------------------------------------| |
| 91 | +| **Role** | Manages a screen and its views | Displays content and handles user input | |
| 92 | +| **MVC Position** | Controller | View | |
| 93 | +| **Lifecycle** | Full lifecycle (`viewDidLoad`, `viewWillAppear`, etc.) | Limited lifecycle (`init`, `draw`, etc.)| |
| 94 | +| **Memory Management** | Manages memory with `didReceiveMemoryWarning` | Managed by parent views | |
| 95 | +| **Event Handling** | Handles high-level app events | Handles touch and gesture events | |
| 96 | +| **Use Case** | For screens/pages | For UI components or subviews | |
| 97 | + |
| 98 | +In short, `UIViewController` handles the logic and flow of a screen, while `UIView` manages rendering and interaction for individual components. |
| 99 | + |
| 100 | + |
| 101 | +# Architecture |
| 102 | +What the difference between MVVM and Clean Architecture ? |
3 | 103 | | Aspect | Clean Architecture | MVVM |
|
4 | 104 | |----------------------------|-------------------------------------------------|--------------------------------------------|
|
5 | 105 | | **Purpose** | Provides a layered approach for organizing code to enhance maintainability, scalability, and testability. | Separates UI logic from business logic to improve the organization of UI-related code. |
|
|
0 commit comments