Posts

Core Data and Concurrency

Image
Introduction :  Up to now, we've used a single managed object context, which we created in the  CoreDataManager  class. This works fine, but there will be times when one managed object context won't suffice. What happens if you access the same managed object context from different threads? What do you expect happens? What happens if you pass a managed object from a background thread to the main thread? Let's start with the basics. Concurrency Basics Before we explore solutions for using Core Data in multithreaded applications, we need to know how Core Data behaves on multiple threads. The documentation is very clear about this. Core Data expects to be run on a single thread. Even though that thread doesn't have to be the main thread, Core Data was not designed to be accessed from different threads. Core Data expects to be run on a single thread. The Core Data team at Apple is not naive, though. It knows that a persistence framework needs to be accessible...

Control Flow For Swift

Swift provides a variety of control flow statements. These include  while  loops to perform a task multiple times;  if ,  guard , and  switch  statements to execute different branches of code based on certain conditions; and statements such as  break  and  continue  to transfer the flow of execution to another point in your code. Swift also provides a  for - in  loop that makes it easy to iterate over arrays, dictionaries, ranges, strings, and other sequences. Swift’s  switch  statement is considerably more powerful than its counterpart in many C-like languages. Cases can match many different patterns, including interval matches, tuples, and casts to a specific type. Matched values in a  switch  case can be bound to temporary constants or variables for use within the case’s body, and complex matching conditions can be expressed with a  where  clause for each case. For-In Loops :   ...