Showing posts with label iOS. Show all posts
Showing posts with label iOS. Show all posts

Friday, April 26, 2019

[iOS] - Semaphore to control access to shared resource


  1. DispatchSemaphore is initialized with maximum number of threads to access the shared resource.
  2. It provides methods wait()/signal() to lock/unlock resource
  3. If the resource access is locked in a lower priority thread by semaphore, it freezes and releases the resource until finish it although the higher priority thread is there, this is called Priority Inversion or Priority Inheritance.

  • Multiple Semaphore access resource wait for each other can cause deadlock

[iOS] - Core Data Notes


  1. Core data is a framework to manage you data graph, not a database. It support data being saved as XML, binary, SQLite and Memory.
  2. One app could have multiple data stores, because for some cases, some data need to store in memory, some need to store in SQLite, etc.
  3. Managed object can't be passed crossing thread, but you can do it by using its ManagedObjectID.
  4. MOC(Managed Object Context) - the data change is only propagated from child to parent and not the other round, and not propagated between siblings.
  5. Multiple thread (concurrency) supports - main queue and private queue in background thread, you have to use perform related methods to save the change if it's in a private queue.
  6. Merge types: as the data may be reading and writing in multiple thread, you should choose data merge type for data propagating. Types includes: trump memory, trump object, override, discard.
  7. Memory and performance
    1. Specify the fields only that you desire.
    2. Result limitation.
    3. Decrease fault overhead by using batch faulting and prefetching.
    4. Check and manage you data in memory like using reset method, etc.
      https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/Performance.html#//apple_ref/doc/uid/TP40001075-CH25-SW1