0918 iOS基础关于Notifications
1、Notifications(link)
??????? --Design patterns for broadcasting information and for subscribing to broadcasts.
??????? 一種關于廣播信息和訂閱廣播信息的設計模式
? NSKeyValueObserving(link)????? //這是協議
--An informal protocol that objects adopt to be notified of changes to the specified properties of other objects.
???? 一種非正式協議,遵循這種協議的對象都可以接收到通知,這個通知是關于 指定的 某對象的 屬性 發生變化時產生的通知,牛逼的是NSObject遵循了該協議,更牛逼的是,大多數的iOS的類都繼承了NSObject,所以大概率下,iOS定義的類都可以接收通知,不是絕對。也有部分并未遵循該協議。實在不行,你自己擴展遵循便是了。這個協議里有很多聲明方法啊。
??????? 關于觀察者模式的編程指南:Key-Value Observing Programming Guide(link)
??
再看一個 象征通知信息 的結構體:
? struct Notification(link)???? //這是結構體
--A container for information broadcast through a notification center to all registered observers.
??? 就是一個結構體,這個結構體是一個容器,容納了各種可以用來廣播出去的通知信息,廣播的媒介NotificationCenter ,通知信息的接收人是所有已經注冊了的觀察者,觀察者就是用上面的NSKeyValueObserving的方法來注冊成觀察者,作為誰的觀察者,去觀察誰這些,還要繼續看NotificationCenter的方法。
??? 例如 addObserver(_:selector:name:object:) 又或者 NSObject 中實現了 NSKeyValueObserving協議的observeValue(forKeyPath:of:change:context:)的方法,反正就有各種方式成為觀察者,或者被觀察
?
? class NotificationCenter??? //這是類
--A notification dispatch mechanism that enables the broadcast of information to registered observers.
????? 沒錯,這就是通知信息的媒介,一種分發信息的機制。可以通過這個類,向所有的在冊的觀察者發送通知。每一個app都有一個default 的 notification center,你可以自定義,沒什么事,默認的就夠用了。但是每一個程序只能擁有一個notification center,所以你要跨程序傳遞通知的話,可以用來DistributedNotificationCenter(link)實現。
介紹一些NotificationCenter 的方法:?????
? func addObserver(Any, selector: Selector, name: NSNotification.Name?, object: Any?)
?--Adds an entry to the notification center's dispatch table with an observer and a notification selector, and an optional notification name and sender.
? ?? 將一個條目entry添加到 notification center 的分發表中,這個條目包括了觀察者、通知選擇器、通知名稱、以及發送者,信息選擇器其實就相當于c語言的函數指針,用來進行響應的操作的,然后通知的名稱是指定該觀察者觀察這個通知,就是通知的標識。? 一個對象可以多次調用次方法,多次指定自己作為觀察者,自己觀察自己。selector的參數,可以根據該方法的調用者進行推斷。
? func post(name: NSNotification.Name, object: Any?)
?--Creates a notification with a given name and sender and posts it to the notification center.
??? 把你定義的notification對象推進center中,輪到它的時候,center就會把它發布出去,然后那些注冊了觀察者的對象就會監聽通知 ,監聽到了,就會進行響應的操作。還有其他一些重載的post函數,邏輯是差不多的。
?
2、然后我也不知道怎么學了,那就閱讀文檔做筆記吧
?
???????? 官網里面有很多文檔的超鏈接的,一個個地挖,慢慢的你就會越挖越多了。
???????? 目前計劃路線:
?????????????????????????????????????????? Notification Programming Topics(link) —>
?????????????????????????????????????????? —> Key-Value Observing Programming Guide(link)—>
?????????????????????????????????????????? —> Threading Programming Guide (link)? —>
????????????????????????????????????????? —> Concurrency Programming Guide(link)—>
?????????????????????????????????????? —> Core Bluetooth Programming Guide(link)
???????? 先看著再說。還有event要看
? 文檔:Notification Programming Topics(link)
? --These recipients of the notification, known as observers, can adjust their own appearance, behavior, and state in response
????? to the event. The object sending (or posting) the notification doesn’t have to know what those observers are.
????? notification 是一個封裝通知的信息的對象,主要被設計用于通知事件的發生。你可以在你的代碼中這樣設計,當某事件發生時, 就發送一個notification,而這個notification會被observers接收,observer就會根據notification作出響應的行為。于是就相當于 observer響應了事件。沒錯,observer也是要你寫代碼注冊的。具體又要看后面的notification center了。后面再說。
? --When a notification is delivered to an interested observer, the notification object is passed in as an argument of the method? handling the notification
★當center把notification傳遞給observer時,notification就作為處理該事件的函數的參數傳進去,也就是selector的參數,可以根據類型推斷。所以你定義的selector中要么參數就只有notification,要么就為空,反正notification都是要被默認傳進去。
? --The method must have the following signature:
| - (void)myNotificationHandler:(NSNotification *)notif; |
? --In this handling method, you can extract information from the notification to help you in your response, especially data in theuserInfo dictionary (if one exists).
???? 這是oc的,所以說swift的也一樣,他會從你的selector方法中抽取出你的notification對象,從而獲取相關信息,所以你定義響應事件的函數時,參數必須有且僅有一個notification,以便于center把notification傳進去你的響應函數中,我說的。
?
?? Notifications
?? --A message is the name of a method, and any parameters associated with it, that are sent to, and executed by, an object.???
??? 在OC中,message指的就是一個函數的名字,你send一個message給一個實例,就是你調用了該實例的該函數,換一個說法而已
? --A notification encapsulates information about an event
???? 一個notification就是壓縮一些關于事件信息的通知對象。
? --An NSNotification object (referred to as a notification) contains a name, an object, and an optional dictionary.
??? 一個NSNotification實例包含了自己唯一的名字,發送者,和補充說明作用的字典
?
?? Notification Centers
??? --It notifies all observers of notifications meeting specific criteria.
?????? center 會將 規范的通知 發送給所有,是所有的觀察者,至于觀察者響應不響應又是另一回事了。
?? --A notification center delivers notifications to observers synchronously. In other words, when posting a notification, control
????? does not return to the poster until all observers have received and processed the notification.
????? 通知是同步地發送到所有觀察者的,也就是說,一個通知都發了個遍了,且都被處理掉了,控制權才會歸還到發布者手里。
? --To send notifications asynchronously use a notification queue, which is described in Notification Queues.
???? 如果你要異步的發送信息,那就使用Notification Queues.這個類
?? --In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may? not be the same thread in which an observer registered itself
????? 特別注意:在多線程的app中,notification只會在發布該notification的線程中傳遞,所以其他線程怎么辦,我也還沒看到啊。
?
?? Notification Queues
????? --The NSNotificationQueue class contributes two important features to the Foundation Kit’s notification mechanism: the? coalescing of notifications and asynchronous posting.
??????? NSNotificationQueue主要是為了兩個功能:合并通知,異步發布通知
??? -- Every thread has a default notification queue, which is associated with the default notification center for the process. You can create your own notification queues and have multiple queues per center and thread
??????? 每一個線程都有自己的notification center,所以你可以為每一個線程創建一個notification queue
?? --However, the invocation of the method is synchronous: before the posting object can resume its thread of execution, it must wait until the notification center dispatches the notification to all observers and returns.
?????? center發布notification的方法仍然是同步的,但是在queue中的notification是一個一個地異步被發布的。
? --These NSNotificationQueue’s methods immediately return to the invoking object after putting the notification in the? queue.
? ? ? NSNotificationQueue 的方法會立即返回值給方法的調用者,然后再異步的發布通知。 ? ?
?? --When the thread where a notification is enqueued terminates before the notification queue posts the notification to its notification center, the notification is not posted
? ? ? 當線程已經終止了,但是綁定該線程的notification還沒有發布到 center 的情況下,這個notification不會再被發布。
? --The mode argument specifies the run loop mode in which the queue will be emptied
? ?? 在enqueuing方法中的mode參數,mode參數指定了notification隊列在哪一種運行模式下會被清空,run loop運行模式是指線程的 運行模式,具體后面看到Threading Programming Guide (link) 會有所介紹。如果該運行模式還沒清空隊列,則等線程再次處于該運行模式下時,再繼續之前的未完成的清空操作。
? --Posting to a notification queue can occur in one of three different styles: NSPostASAP, NSPostWhenIdle, and NSPostNow.
? ?? 向notification queue隊列里發布notification有三種方式,或者說三種狀態下向隊列發布通知:NSPostASAP, NSPostWhenIdle,? and NSPostNow,記住,仍然是center從queue里面拿notification發布給observer監聽。現在是sender發布給queue而已。
?? Registering for a Notification
???? --You don’t need to specify both the name and the object. If you specify only an object, the observer will receive all? notifications containing that object. If you specify only a notification name, the observer will receive that notification every time it’s posted, regardless of the object associated with it.
???????? 在注冊觀察者時,你不需要強制指定notification中的object屬性,可有可無。有name時,則observer根據name監聽? notification,無name時,則根據object監聽notification
??? --Before an object that is observing notifications is deallocated, it must tell the notification center to stop sending it ? notifications. Otherwise, the next notification gets sent to a nonexistent object and the program crashes.
?????? 在oc的版本中,當一個notification的所有觀察者都已經被銷毀了時,那么你的notification也必須要從center中移除,因為center 是等到當前的通知全部發了了個遍并且返回值時才廣播下一條通知,所以如果沒有observer返回的話,center會一直等待,會造成程序的崩潰。在swift版本中,好像不會等待還是怎么,反正作了優化。
? ? Posting a Notification
????? --NSNotification objects are immutable, so once created, they cannot be modified.
???????? notification是不可變的,一旦被創建,你就不可以改變notification。
????? --If there are other objects of interest to the observer (besides the notification name and observed object), place them in the notification’s optional dictionary or use postNotificationName:object:userInfo:.
??????? 如果還有一些對象不是觀察者,又對notification感興趣的話,你可以把這些對象添加到notification的可選字典中,或者用postNotificationName:object:userInfo:方法來通知該對象。
? ? Delivering Notifications To Particular Threads
????? --You register for a notification normally. When a notification arrives, you test whether the current thread is the thread that should handle the notification. If it is the wrong thread, you store the notification in a queue and then send a signal to the correct thread, indicating that a notification needs processing. The other thread receives the signal, removes the notification from the queue, and processes the notification.
???????? 跨線程通知的工作邏輯是:你要自定義一個a custom notification queue (not an NSNotificationQueue object),而不是使用默認的notification隊列,然后你還是依照往常那樣定義一個notification,當notification到達你定義的隊列時,你會測試它是不是要在當前運行的線程中廣播的,如果是,則廣播出去。
??????? 如果不是,你就把這個notification存儲在隊列中,然后告知目標線程來處理它。目標線程收到告知后,就會從隊列中移除并拿出該notification,然后就進行相應的處理。
? ? --To implement this technique, your observer object needs to have instance variables for the following values: a mutable array to hold the notifications, a communication port for signaling the correct thread (a Mach port), a lock to prevent? multithreading conflicts with the notification array, and a value that identifies the correct thread (an NSThread object).
???????? 為實現上面的工作邏輯,你的線程級別的觀察者實例需要的東西有:可變的notification數組(用于儲存notification,廣播給當前線程的普通級別的觀察者,普通與線程級別區別在于你想怎么用這個observer)、一個Mach端口實例(主要是為了給別的 線程提供訪問)、一個同步鎖實例(主要是為了同步notification數組,避免線程沖突)、還有該線程的唯一標識。
?
? 文檔:? Introduction to Key-Value Observing Programming Guide(link)
?? Introduction
--Key-value observing is a mechanism that allows objects to be notified of changes to specified properties of other objects.
?? 鍵值監聽是一種機制,這種機制下,一個對象可以監聽到另一個對象的 特定的 屬性的 改變,從而作出相應的響應。
--In order to understand key-value observing, you must first understand key-value coding.
?? 要了解 鍵值監聽 機制,你必須要先了解 鍵值對編程 思想。
?? KVC是key-value coding , KVO是key-value observing
--Key-value observing is particularly useful for communication between model and controller layers in an application
??? 鍵值監聽機制 在app的model層和controller層特別好用。然后下面大概說一下MVC設計模式。
▼ MVC:
? --The The Model-View-Controller (MVC) design pattern defines not only the roles objects play in the application, it defines the way objects communicate with each other.
??? MVC設計模式不僅定義了對象在app中扮演的角色,還定義了這些角色之間該如何進行交流。
? --Model objects encapsulate the data specific to an application and define the logic and computation that manipulate and process that data
??? Model層對象封裝了app中特定的某種數據,并且定義了操作和處理該數據的操作邏輯和計算方式。
? --Because model objects represent knowledge and expertise related to a specific problem domain, they can be reused in similar problem domains.
?? 因為Model層象征著特定行業領域的 相關知識和專業知識 ,所以Model層的對象能夠在這些領域中復用,所以你應該把那些從數據庫或者本地文件加載到app中的 那些persistent state的 數據儲存在model層的對象中。
? --Ideally, a model object should have no explicit connection to the view objects that present its data and allow users to edit that data—it should not be concerned with user-interface and presentation issues.
??? 理想情況下,model層的對象不能與view層的對象有明確的直接聯系,而是應該通過controller層進行聯系,view層的對象是讓客戶覺得自己能在界面上編輯數據而已,并且覺得數據就展示在界面上,只是讓客戶感覺能直接操作數據而已。實際上還要通過 controller層才能確定客戶(view層)能不能操作model層的數據,以及怎么去操作數據(交互)
? --A view object knows how to draw itself and can respond to user actions. A major purpose of view objects is to display data from the application’s model objects and to enable the editing of that data.
??? view層的對象應該要知道怎么繪制自己的視圖,然后還要讓客戶感覺自己在直接操作model層的數據,實際上view層和model層是解藕的,他們必須通過controller層,才可以通信。
? --Controller objects are thus a conduit through which view objects learn about changes in model objects and vice versa
??? controller層就相當于一條管道,view層對象可以通過這條管道了解到model層數據的變化,反之亦然。
? --The Cocoa frameworks offer three main controller types: coordinating controllers, view controllers (on iOS), and mediating controllers (on OS X).
? ? Cocoa frameworks 提供了三種類型的controller,一個是面向app級別的coordinating controller,主要處理實例的生命周期,事件的響應,實例之間的連接關系的邏輯;一個是view controller,主要是處理iOS中model 層和view層之間的數據交互,OS X也有view controller,不關我事。還有一個是mediating controller,也是OS X的事情。
▲ MVC - END
-- A controller object typically observes properties of model objects, and a view object observes properties of model objects through a controller.
??? 一個controller對象監聽model對象,而view對象則監聽controller對象,而model對象則監聽model對象,這是基本的模式。
--KVO’s primary benefit is that you don’t have to implement your own scheme to send notifications every time a property changes.
?? KVO機制的主要好處在于,你不必每次都為你的 對象的 property性質制定一個發送通知的計劃,就是你不用每次property發生改變時,都手動地寫代碼去發送通知,而是KVO機制幫你完成了這些步驟,你要做的事是注冊和注銷observer。
?? 至于KVO是怎么幫你做的,看下面的Registering for Key-Value Observing。需要知道的是,property屬性是遵守KVO Compliant的,而NSObject也是遵守KVO Compliant的,所以你的對象繼承NSObject就好了啦。
? Registering Dependent Keys
--You must perform the following steps to enable an object to receive key-value observing notifications for a KVO-compliant property:
??? 如果你要你的觀察者對象能夠接受到別的對象的property發生改變時的通知,那么必須包含三步:一是注冊觀察者,二是響應通知的方法,三是注銷觀察者。并非cocoa里所有的class都是KVO Compliant,只有在文檔中說明它是KVO Compliant的,才是KVO Compliant的
--Register the observer with the observed object using the method addObserver:forKeyPath:options:context:.
?? 要注冊一個觀察者(包含被監聽的對象),用方法addObserver:forKeyPath:options:context:.
? 其中的option參數:
-- affects both the content of the change dictionary supplied in the notification, and the manner in which notifications are generated.
?? 影響了 變更字典 的內容,也影響了notification的生成方式。例如:當option取值NSKeyValueObservingOptionOld時,表示你要獲取被監聽對象的property的舊值;當取值NSKeyValueObservingOptionNew,表示你要取新值;當取值OR時,表示你新舊都要;
?取值NSKeyValueObservingOptionInitial,表示你要求被觀察對象的property只要發生了改變,就立馬發通知過來;當取值NSKeyValueObservingOptionPrior,表示你要在被觀察對象的property發生改變之前就發通知過來,一般是指被觀察對象調用了以-willChange…為前綴的方法時,用change dictionary儲存該通知,相當于willset吧,我猜的。
? 然后就是context參數:
--You may specify NULL and rely entirely on the key path string to determine the origin of a change notification, but this approach may cause problems for an object whose superclass is also observing the same key path for different reasons.
? 你可以設置context參數為空,并且完全依賴于key path的變化來確定notification的來源,但是如果observer的父類也監聽這個key path的話,就有可能會產生一些問題。說一下key path,就是key的完整路徑名,例如com.csdn.http ,則當這三個對象中的任意一個對象的property性質發生變化,都會發布notification。
? 好了,context參數可以包含了任意數據類型,這些數據是用于返回給 observer 的,context是一個指針,上下文的指針,它可以用于識別該通知是給你的還是給你父類的,具體做法之一是把context賦值給你的類中的一個靜態變量。好像這里的key path只是單純的作為字符串使用,作為notification的唯一標識。
--When the value of an observed property of an object changes, the observer receives an observeValueForKeyPath:ofObject:change:context: message. All observers must implement this method.
? 當被觀察的property值發生變化時,所有的observer都會通過observeValueForKeyPath:ofObject:change:context:獲取到相應的通知,所以observer必須實現這個方法。還有就是,context和observer還有observed 對象之間默認不是強引用的,如果你要用到強引用,請自己定義。
? 然后就是change參數:
--The change dictionary entry NSKeyValueChangeKindKey provides information about the type of change that occurred.
?? change dictionary的entry提供了關于變化的類型的信息,entry中的key可以是變化的種類,value可以是property的新舊值。
--If you used a single context for all observed key paths, you first test that against the notification’s context, and finding a match, use key path string comparisons to determine what specifically has changed.
? 如果你使用了context指針,那么observer會先匹配context,然后再匹配key path,從而確定你的notification是否是你想要的。
--If a notification propagates to the top of the class hierarchy, NSObject throws an NSInternalInconsistencyException because this is a programming error: a subclass failed to consume a notification for which it registered.
? 如果一個notification傳遞到了class的最頂層,即NSObject,則系統會拋出異常,因為沒有子類消費這個通知。
? KVO Compliance
?--In order to be considered KVO-compliant for a specific property, a class must ensure the following:
??? 為了使class的property符合KVO規范,class必須遵守這三個原則:一是,property必須是KVC的,即符合key-value編程的,就是鍵值對啊;二是,在property發生變化時,class必須發出相應的變化通知;三是,如果是獨立key,即不經過center的notification的key,則必須按照獨立key的規范進行注冊。
--Automatic support and Manual change notification techniques ensures the change notifications are emitted.
?? 自動支持技術和人工通知變化的技術,保證了 關于變化的通知 能被發出去。?
--Automatic key-value change notification informs observers of changes made using key-value compliant accessors, as well as the key-value coding methods.
?? 自動通知技術,如果property是通過 accessors方法或者是KVC方法 而發生變化時,自動通知技術就會去通知監聽這個property的觀察者們,所以如果你要使用自動通知技術,那么你的class和property就必須遵循cocoa 的編程規范和命名規范。
--Manual change notification provides more controls of the notification process .Manual and automatic notifications are not mutually exclusive.
? 手動的變化通知,提供了更多的關于通知處理的控制權,而且手動通知和自動通知并不互斥,你可以同時使用。
--More typically, you may want to completely take control of the notifications for a particular property. In this case, you override the NSObject implementation of automaticallyNotifiesObserversForKey:
?如果你想擁有 處理通知的 更多控制權,你可以重寫父類的automaticallyNotifiesObserversForKey: 方法,從而禁止或者修改 自動通知的 處理操作,你別禁止所有的key監聽啊,記得繼承父類的啊,禁止你自己的key就好了。
--To implement manual observer notification, you invoke willChangeValueForKey: before changing the value, and didChangeValueForKey: after changing the value.
?? 為了實現 手動通知 observer的功能,你需要在property改變的前后分別調用willChangeValueForKey: 和 willChangeValueForKey: 方法,來向observer發送修改前后的通知。這兩個方法時遵循了KVO規范的類都默認繼承了的。
?Registering Dependent Keys
--If the value of one attribute changes, then the value of the derived property should also be flagged for change.
??? 如一個屬性改變,那么與這個屬性有依賴關系的 其他對象的 property性質也應該被標記,說property也發生了改變,因為它的依賴發生了改變。但是如何去通知這種改變呢,就取決于依賴關系的基數了。也就是在說一對多,多對多的那種依賴。
--To trigger notifications automatically for a to-one relationship you should either override? keyPathsForValuesAffectingValueForKey: or implement a suitable method that follows the pattern it defines for registering dependent keys
??? 為了出發一對一關系的自動通知功能,你可以重寫keyPathsForValuesAffectingValueForKey:方法,或者實現一個遵循了“依賴鍵注冊規范”的方法,這個規范的方法名是:keyPathsForValuesAffectingPropertyName,其中方法名后面的那個PropertyName就是你的property的name,但要求把首字母變成大寫的。其實和java的setter,getter的方法名規范類似。這是OC版本的。
--You can't override the keyPathsForValuesAffectingValueForKey: method when you add a computed property to an
?? existing class using a category, because you're not supposed to override methods in categories.
?? 你不能在OC的category中重寫 keyPathsForValuesAffectingValueForKey:方法,OC中的category就相當于swift中的extension,OC是禁止這種行為的。所以實現依賴方法此時就很有用武之地了。
--You can‘t set up dependencies on to-many relationships by implementing keyPathsForValuesAffectingValueForKey:.
?? Instead, you must observe the appropriate attribute of each of the objects in the to-many collection and respond to changes
?? in their values by updating the dependent key yourself.
? ? 你不能通過重寫keyPathsForValuesAffectingValueForKey:方法來實現多對多關系的通知。因為你需要通知每一個在多對多關系中有所涉及的property,這是不可取的。
--You can use key-value observing to register the parent (in this example, Department) as an observer of the relevant attribute
??? of all the children (Employees in this example).
??? 在一對多關系中,例如部門和職員,你可以將 部門對象 注冊為觀察者,而每個 職員對象 作為被觀察者。
?
? 文檔: Threading Programming Guide(link)
?? Introduction
--Although newer technologies such as operation objects and Grand Central Dispatch (GCD) provide a more modern and? efficient infrastructure for implementing concurrency, OS X and iOS also provide interfaces for creating and managing threads.
??? 雖然一些更加新的技術也提供了很好的架構來實現程序的并行,例如 operation objects 和 CGD 技術,但是,iOS同樣也提供了創建和管理線程的接口技術。
--Threads are one of several technologies that make it possible to execute multiple code paths concurrently inside a single application.
??? 線程是在一個app里執行 多個路徑的代碼 的技術。
--These alternative technologies simplify the amount of work you have to do to implement concurrent paths of execution and offer much better performance than traditional threads. For information about these technologies, see Concurrency Programming Guide
??? 那些可替代線程的 新的 并行的 開發技術,可以大量簡化你的工作量,并且擁有更高的效率。具體看文檔:Concurrency ? Programming Guide(link)
?
??About Threaded Programming
--Threads are a relatively lightweight way to implement multiple paths of execution inside of an application
??? 線程是一種 在一個app中的 實現多條執行路徑的 相對輕量的 方式。
--From a technical standpoint, a thread is a combination of the kernel-level and application-level data structures needed to manage the execution of code.
?? 從技術角度來講,一個線程就是 內核級別和app級別的 數據結構的 結合體,用于管理代碼的執行。
--The kernel-level structures coordinate the dispatching of events to the thread and the preemptive scheduling of the thread on one of the available cores.
??? 內核級別的數據結構用于 協調事件如何分發到線程 以及 在一個可用的內核上如何搶占式地調度線程。
--The application-level structures include the call stack for storing function calls and the structures the application needs to manage and manipulate the thread’s attributes and state.
??? app級別的數據結構則是包含了 方法棧、管理app的數據結構,同時也管理線程的屬性與狀態。
--Although the underlying implementation mechanism for threads is Mach threads, you rarely (if ever) work with threads at the Mach level. Instead, you usually use the more convenient POSIX API or one of its derivatives.
?? 雖然線程的實現機制的底層支持是Mach thread (即機器級別的線程),但是你一般不會用到Mach級別的線程,而是用到POSXI
?? API 或者POSIX API 的某個衍生接口;POSIX意思是:可移植的計算機操作系統接口。
--When you create a new thread, you must specify an entry-point function (or an entry-point method in the case of Cocoa threads) for that thread.
?? 當你創建一個新的線程時,你必須指定一個入口函數;在這個入口函數中,你需要定義大量的工作或者循環的工作,因為這個入口
?? 函數一旦返回,意味著你已經 顯式地(或系統)終結了 該線程,這個線程的資源將被系統回收,也就是不復存在了。
--A run loop is a piece of infrastructure used to manage events arriving asynchronously on a thread
?? 運行循環 是一種基礎設施,該設施主要用于管理 事件 異步到達 線程 的相關事項
--As events arrive, the system wakes up the thread and dispatches the events to the run loop, which then dispatches them to the handlers you specify
?? 當實現到達時,系統將喚醒相關的線程,并且把事件分發到 運行循環 ,運行循環再把事件分發給你定義的 處理程序中。
--To configure a run loop, all you have to do is launch your thread, get a reference to the run loop object, install your event handlers, and tell the run loop to run.
?? 配置run loop : 啟動線程,獲取run loop對象的引用,安裝事件處理程序,通知run loop運行起來。
--When maintaining completely separate resources is not an option though, you may have to synchronize access to the resource using locks, conditions, atomic operations, and other techniques.
?? 當不用完全獨立的擁有和維護資源時,你可能必須要通過鎖、條件、原子操作和其他同步技術來對資源進行同步了。
--If you use operation objects, you can configure dependencies among your operation objects to sequence the execution of tasks, which is very similar to the behavior offered by conditions.
?? 如果你使用 操作對象 的話,那么你可以配置 操作對象 之間的依賴關系,這樣就可以對任務的執行進行排序了。這種做法非常類
?? 似于 條件同步 的做法
--Atomic operations use special hardware instructions to ensure that modifications to a variable are completed before other threads have a chance to access it.
?? 原子操作 使用了特殊的硬件指令,這些指令保證了 在別的線程訪問該變量時,該變量已經被完整地修改完畢了。
--Rather than create a thread yourself, consider using asynchronous APIs, GCD, or operation objects to do the work
?? 盡量不要自己顯式地創建一個線程,而是使用系統提供的 異步API,GCD,或者操作對象 來實現一個線程。
--Changing your code to a transaction-based model to compensate could subsequently negate the performance advantage of having multiple threads
?? 如果將你的代碼改成基于事務模型的編程,以此來抵償資源爭奪的問題,雖然這樣解決了資源爭奪問題,但同時也消除了多線程模式帶來的優勢。所以多線程就很難啊
--A process runs until all non-detached threads have exited. By default, only the application’s main thread is created as non-detached, but you can create other threads that way as well
??? 一個進程會一直運行到所有非分離的線程都退出為止,此時的進程才結束。非分離即是緊挨著咯。默認地,主線程都是非分離的,雖然你也可以自己創建非分離的線程。分離的線程就是獨立的線程的意思咯。
--Because most high-level thread technologies do not create joinable threads by default, you may have to use the POSIX API to create your thread.
?? 因為大多數的高級線程技術都不會默認創建非分離線程,所以你必須自己動手顯示的創建。因而你需要通過高POSIX API 來創建你的非分離線程,非分離線程的作用是你的線程存在時,app不會退出,獨立線程的話,app可能直接就退出了。所以非分離線程傳輸數據的話,可以保證了數據傳輸時不被終結。non-detached (also known as joinable),非分離也叫可加入的
--Because each thread has its own call stack, each thread is therefore responsible for catching its own exceptions.
?? 每一個線程都有自己的 當前調用棧 ,所以每個線程都要自己捕捉自己拋出的異常。
--The best way for a thread to exit is naturally, by letting it reach the end of its main entry point routine
?? 最好的終結線程的方式是 讓線程自然地跑到自己的路線的盡頭。
?
? Thread Management
--Each process (application) in OS X or iOS is made up of one or more threads, each of which represents a single path of execution through the application's code
?? OS X或iOS中的每個進程(應用程序)由一個或多個線程組成,而每個線程都代表了 單一路徑的用于執行的代碼。
--Each thread requires the allocation of memory in both the kernel memory space and your program’s memory space.
?? 每一個線程都需要你給它分配 內核內存空間 和 程序內存空間 ,這是實實在在的內存成本。內核空間存儲用于調度和管理的核心數據結構,程序內存空間用于儲存每個線程的必要數據。
--Operation objects can often create threads more quickly,they use pools of threads already residing in the kernel to save on allocation time
?? 操作對象 能夠更快地創建線程,因為它們使用的是線程池技術,即再次利用殘留的線程,而不是從頭創建新線程。
--A detached thread means that the thread’s resources are automatically reclaimed by the system when the thread exits.
?? 一個獨立線程意味著 在獨立線程退出后,它使用的資源會被系統自動回收。
--There are two ways to create a thread using the NSThread class:
-
Use the detachNewThreadSelector:toTarget:withObject: class method to spawn the new thread.
-
Create a new NSThread object and call its start method. (Supported only in iOS and OS X v10.5 and later.)
??? 這是創建獨立線程的兩種方式,一個是直接調用NSThread的類方法detachNewThreadSelector:toTarget:withObject:,
??? 另一種方式是創建一個新的NSThread對象
--If you have an NSThread object whose thread is currently running, one way you can send messages to that thread is to use the performSelector:onThread:withObject:waitUntilDone: method of almost any object in your application.
?? 如果有一個正在運行的NSThread甲線程,而你要從另外一個乙線程發消息給甲線程,有一種方式是,你通過調用對象中的 performSelector:onThread:withObject:waitUntilDone: 方法,你的app中幾乎每一個對象中都會有這個方法。這種方式適用于線程間的偶爾通信,如果是頻繁通信或者是敏感通信,則不建議使用這種方式。
--OS X and iOS provide C-based support for creating threads using the POSIX thread API ;? POSIX creates threads as joinable by default, this example changes the thread’s attributes to create a detached thread .
?? POSIX技術是基于C語言的,所以它可以創建出適用于iOS和OS X系統的線程,他默認創建的線程是非分離的,但是你可以在創建時改變線程的屬性,使該線程是分離的(即獨立的)。
--The LaunchThread function creates a new thread whose main routine is implemented in the PosixThreadMainRoutine function
?? 好吧,POSIX創建線程是通過LaunchThread函數創建的,而這個函數的主線程是在PosixThreadMainRoutine函數中實現的。
--The POSIX routine you use to create threads is called, appropriately enough, pthread_create.
?? 你用來創建線程的? POSIX例程 叫做 pthread_create.
--In iOS and OS X v10.5 and later, all objects have the ability to spawn a new thread and use it to execute one of their methods. The performSelectorInBackground:withObject: method creates a new detached thread and uses the specified method as the entry point for the new thread.
? 在新版的iOS系統中,所有對象都可以新建一個分離式線程,通過performSelectorInBackground:withObject: 方法即可,這個方法是NSOject中的。
--To let Cocoa know that you intend to use multiple threads, all you have to do is spawn a single thread using the NSThread class and let that thread immediately exit
?? 如果要使cocoa知道你想要的是多線程編程,你只需要新建一個單線程,然后立馬退出該線程。那么cocoa就明了了。POSIX技術
--If you are not sure if Cocoa thinks your application is multithreaded or not, you can use the isMultiThreaded method of NSThread to check.
?? 如果你還心存疑慮cocoa不知道你是多線程,那么你就用NSThread中的isMultiThreaded方法來判斷吧。
--In iOS and OS X v10.5 and later, allocate and initialize an NSThread object. Before calling the start method of the thread object, use the setStackSize: method to specify the new stack size.
?? 在新版本iOS中,你可以用NSThread創建一個線程,然后你要修改棧大小的話,你必須在調用start方法之前用setStackSize:
?方法來修改棧大小。用 POSIX 技術 則是你可以定義一個數據結構來決定棧的大小。
--Cocoa and POSIX store the thread dictionary in different ways, so you cannot mix and match calls to the two technologies
?? 線程會維護一個 字典 作為存儲機制,你可以在線程的整個run loop的任何地方訪問這個字典,但是Cocoa 和 POSIX 技術儲存這個字典的方式是不一樣的,所以你要區分在兩種技術中對 字典 訪問時的訪問方法不一樣。
--Although they still run as independent threads, a joinable thread must be joined by another thread before its resources can be reclaimed by the system.
?? 結合線程 雖然仍讓向獨立線程一樣運行,但是一個結合線程在被系統回收資源之前,必須先被另外一個線程接入,即是另外一個線程對你的資源進行回收處理。
--Each joinable thread must be joined before the process is allowed to exit
?? 在進程退出之前,它的每個結合線程都必須已經被鏈接了,所以 結合線程 適用于敏感的、不能被打斷的工作場景。
--If you do want to create joinable threads, the only way to do so is using POSIX threads. POSIX creates threads as joinable by default.
?? 你只能通過POSIX技術來創建結合線程。它默認就是創建結合線程。
--You initialize your data structures, do some work or optionally set up a run loop, and clean up when your thread’s code is done.
?? 寫一個 線程入口點的慣例 是:初始化數據結構、設置run loop的工作內容、清除數據。
--The app frameworks start the run loop of your application’s main thread automatically. If you create any secondary threads, you must configure the run loop and start it manually
?? app默認自動啟動你的主線程的run loop ,如果你想創建第二個線程,那么你必須手動寫它的run loop 。
? Run Loops
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的0918 iOS基础关于Notifications的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络安全-防火墙技术的具体应用
- 下一篇: Raptor-初始化数组的四种方式