导航:首页 > 网络共享 > ios网络请求发生在哪个线程

ios网络请求发生在哪个线程

发布时间:2022-05-18 18:59:15

㈠ 在iOS开发为什么使用多线程 多线程有哪些

1、概念性知识:iOS开发中有主线程(也可叫UI)线程和子线程(你自己通过方法开辟的线程)的概念,子线程和主线程的合称可以称为多线程。
2、为什么用多线程开发:正常一个简单的软件,如果没有比较消耗性能设时间的操作是无需开辟子线程的,我们去一条线程(主线程)上去添加UI、加载数据、刷新,很快,因为加载的数据其实是很小的。但是如果有了诸如图片下载,视频下载等需要花费比较多时间的操作就必须开辟子线程了,图片、视频比较大,下载浪费时间,如果主线程下载,那么整个APP的界面就不动了,因为线程正在忙着下载呢,没时间去刷新UI、响应用户任务(如点击手势等)和加载数据给用户看,APP在辛苦的下载工作,但是用户一看,这软件不动了,以为卡死了,就关了或者卸载了,这样的体验不是我们想要的,所以开辟子线程相当于喊来一个朋友,让他去下载,你可以继续做刷新UI、响应用户任务(如点击手势等)和加载数据给用户看等事情,互不影响,效率提高了,代价是多消耗了写CPU。
3、常规使用多线程有哪些:NSThread , NSOperation(NSOperationQueue) , GCD主要3种,后两种使用较多。苹果推荐使用NSOperation,因为它是面向对象的,和oc一样,换可以控制线程的开始,暂停,取消。GCD这样是C语言函数,函数略生涩,使用很方便,可以实现延时,单次,调度组执行,具体实现网上有很多帖子。

㈡ ioshttp请求有必要在子线程吗

会堵塞吧,比如说网络这会不好,你在主线程里做了同步的http的请求,在返回结果之前,你的主线程就会一直卡在这里,不响应用户的任何操作请求。所以一般都是挂个异步操作或者开个线程去进行http请求

㈢ iOS 网络请求有哪些方式

两种请求方式GET,POST
两种请求方式的比较
相同点:都能给服务器传输数据

不同点:
1、给服务器传输数据的方式:
GET:通过网址字符串。POST:通过data
2、传输数据的大小:GET:⽹址字符串最多255字节。POST:使用NSData,容量超过1G
3、安全性:GET:所有传输给服务器的数据,显示在网址里,类似于密码的明文输入,直接可见。
POST:数据被转成NSData(二进制数据),类似于密码的密文输⼊入,⽆无法直接读取。
连接方式

同步:使用一个线程(主线程)完成所有的工作,效率低,当线程正在执行一个任务的时候无法执行另一个任务,所有如果使用同步进行网络数据的请求,那么在该线程进行网络请求时,暂时无法响应用户的点击事件,用户体验极差
异步:再开一个线程(子线程)去完成任务,此时,主线程依然可以监听用户的点击事件,不会造成卡顿,用户体验较好

㈣ iOS中加载网络数据一定要开子线程吗

不一定啊,想开就开,现在很多网络默认都是多线程

㈤ ios 开发 两个线程怎么通讯

先来看看官方的文档,是这样写的: 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. 翻译过来是: 在多线程应用中,Notification在哪个线程中post,就在哪个线程中被转发,而不一定是在注册观察者的那个线程中。 也就是说,Notification的发送与接收处理都是在同一个线程中。为了说明这一点,我们先来看一个示例: 代码清单1:Notification的发送与处理 @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"current thread = %@", [NSThread currentThread]); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:TEST_NOTIFICATION object:nil]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [[NSNotificationCenter defaultCenter] postNotificationName:TEST_NOTIFICATION object:nil userInfo:nil]; }); } - (void)handleNotification:(NSNotification *)notification { NSLog(@"current thread = %@", [NSThread currentThread]); NSLog(@"test notification"); } @end 其输出结果如下: 2015-03-11 22:05:12.856 test[865:45102] current thread = {number = 1, name = main} 2015-03-11 22:05:12.857 test[865:45174] current thread = {number = 2, name = (null)} 2015-03-11 22:05:12.857 test[865:45174] test notification 可以看到,虽然我们在主线程中注册了通知的观察者,但在全局队列中post的Notification,并不是在主线程处理的。所以,这时候就需要注意,如果我们想在回调中处理与UI相关的操作,需要确保是在主线程中执行回调。 这时,就有一个问题了,如果我们的Notification是在二级线程中post的,如何能在主线程中对这个Notification进行处理呢?或者换个提法,如果我们希望一个Notification的post线程与转发线程不是同一个线程,应该怎么办呢?我们看看官方文档是怎么说的: For example, if an object running in a background thread is listening for notifications from the user interface, such as a window closing, you would like to receive the notifications in the background thread instead of the main thread. In these cases, you must capture the notifications as they are delivered on the default thread and redirect them to the appropriate thread. 这里讲到了“重定向”,就是我们在Notification所在的默认线程中捕获这些分发的通知,然后将其重定向到指定的线程中。 一种重定向的实现思路是自定义一个通知队列(注意,不是NSNotificationQueue对象,而是一个数组),让这个队列去维护那些我们需要重定向的Notification。我们仍然是像平常一样去注册一个通知的观察者,当Notification来了时,先看看post这个Notification的线程是不是我们所期望的线程,如果不是,则将这个Notification存储到我们的队列中,并发送一个信号(signal)到期望的线程中,来告诉这个线程需要处理一个Notification。指定的线程在收到信号后,将Notification从队列中移除,并进行处理。 官方文档已经给出了示例代码,在此借用一下,以测试实际结果: 代码清单2:在不同线程中post和转发一个Notification @interface ViewController () @property (nonatomic) NSMutableArray *notifications; // 通知队列 @property (nonatomic) NSThread *notificationThread; // 期望线程 @property (nonatomic) NSLock *notificationLock; // 用于对通知队列加锁的锁对象,避免线程冲突 @property (nonatomic) NSMachPort *notificationPort; // 用于向期望线程发送信号的通信端口 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"current thread = %@", [NSThread currentThread]); // 初始化 self.notifications = [[NSMutableArray alloc] init]; self.notificationLock = [[NSLock alloc] init]; self.notificationThread = [NSThread currentThread]; self.notificationPort = [[NSMachPort alloc] init]; self.notificationPort.delegate = self; // 往当前线程的run loop添加端口源 // 当Mach消息到达而接收线程的run loop没有运行时,则内核会保存这条消息,直到下一次进入run loop [[NSRunLoop currentRunLoop] addPort:self.notificationPort forMode:(__bridge NSString *)kCFRunLoopCommonModes]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(processNotification:) name:@"TestNotification" object:nil]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [[NSNotificationCenter defaultCenter] postNotificationName:TEST_NOTIFICATION object:nil userInfo:nil]; }); } - (void)handleMachMessage:(void *)msg { [self.notificationLock lock]; while ([self.notifications count]) { NSNotification *notification = [self.notifications objectAtIndex:0]; [self.notifications removeObjectAtIndex:0]; [self.notificationLock unlock]; [self processNotification:notification]; [self.notificationLock lock]; }; [self.notificationLock unlock]; } - (void)processNotification:(NSNotification *)notification { if ([NSThread currentThread] != _notificationThread) { // Forward the notification to the correct thread. [self.notificationLock lock]; [self.notifications addObject:notification]; [self.notificationLock unlock]; [self.notificationPort sendBeforeDate:[NSDate date] components:nil from:nil reserved:0]; } else { // Process the notification here; NSLog(@"current thread = %@", [NSThread currentThread]); NSLog(@"process notification"); } } @end 运行后,其输出如下: 2015-03-11 23:38:31.637 test[1474:92483] current thread = {number = 1, name = main} 2015-03-11 23:38:31.663 test[1474:92483] current thread = {number = 1, name = main} 2015-03-11 23:38:31.663 test[1474:92483] process notification 可以看到,我们在全局dispatch队列中抛出的Notification,如愿地在主线程中接收到了。 这种实现方式的具体解析及其局限性大家可以参考官方文档Delivering Notifications To Particular Threads,在此不多做解释。当然,更好的方法可能是我们自己去子类化一个NSNotificationCenter,或者单独写一个类来处理这种转发。 NSNotificationCenter的线程安全性 苹果之所以采取通知中心在同一个线程中post和转发同一消息这一策略,应该是出于线程安全的角度来考量的。官方文档告诉我们,NSNotificationCenter是一个线程安全类,我们可以在多线程环境下使用同一个NSNotificationCenter对象而不需要加锁。原文在Threading Programming Guide中,具体如下: The following classes and functions are generally considered to be thread-safe. You can use the same instance from multiple threads without first acquiring a lock. NSArray ... NSNotification NSNotificationCenter 我们可以在任何线程中添加/删除通知的观察者,也可以在任何线程中post一个通知。 NSNotificationCenter在线程安全性方面已经做了不少工作了,那是否意味着我们可以高枕无忧了呢?再回过头来看看第一个例子,我们稍微改造一下,一点一点来: 代码清单3:NSNotificationCenter的通用模式 @interface Observer : NSObject @end @implementation Observer - (instancetype)init { self = [super init]; if (self) { _poster = [[Poster alloc] init]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:TEST_NOTIFICATION object:nil] } return self; } - (void)handleNotification:(NSNotification *)notification { NSLog(@"handle notification "); } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end // 其它地方 [[NSNotificationCenter defaultCenter] postNotificationName:TEST_NOTIFICATION object:nil]; 上面的代码就是我们通常所做的事情:添加一个通知监听者,定义一个回调,并在所属对象释放时移除监听者;然后在程序的某个地方post一个通知。简单明了,如果这一切都是发生在一个线程里面,或者至少dealloc方法是在-postNotificationName:的线程中运行的(注意:NSNotification的post和转发是同步的),那么都OK,没有线程安全问题。但如果dealloc方法和-postNotificationName:方法不在同一个线程中运行时,会出现什么问题呢? 我们再改造一下上面的代码: 代码清单4:NSNotificationCenter引发的线程安全问题 #pragma mark - Poster @interface Poster : NSObject @end @implementation Poster - (instancetype)init { self = [super init]; if (self) { [self performSelectorInBackground:@selector(postNotification) withObject:nil]; } return self; } - (void)postNotification { [[NSNotificationCenter defaultCenter] postNotificationName:TEST_NOTIFICATION object:nil]; } @end #pragma mark - Observer @interface Observer : NSObject { Poster *_poster; } @property (nonatomic, assign) NSInteger i; @end @implementation Observer - (instancetype)init { self = [super init]; if (self) { _poster = [[Poster alloc] init]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:TEST_NOTIFICATION object:nil]; } return self; } - (void)handleNotification:(NSNotification *)notification { NSLog(@"handle notification begin"); sleep(1); NSLog(@"handle notification end"); self.i = 10; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; NSLog(@"Observer dealloc"); } @end #pragma mark - ViewController @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; __autoreleasing Observer *observer = [[Observer alloc] init]; } @end 这段代码是在主线程添加了一个TEST_NOTIFICATION通知的监听者,并在主线程中将其移除,而我们的NSNotification是在后台线程中post的。在通知处理函数中,我们让回调所在的线程睡眠1秒钟,然后再去设置属性i值。这时会发生什么呢?我们先来看看输出结果: 2015-03-14 00:31:41.286 SKTest[932:88791] handle notification begin 2015-03-14 00:31:41.291 SKTest[932:88713] Observer dealloc 2015-03-14 00:31:42.361 SKTest[932:88791] handle notification end (lldb) // 程序在self.i = 10处抛出了"Thread 6: EXC_BAD_ACCESS(code=EXC_I386_GPFLT)" 经典的内存错误,程序崩溃了。其实从输出结果中,我们就可以看到到底是发生了什么事。我们简要描述一下: 当我们注册一个观察者是,通知中心会持有观察者的一个弱引用,来确保观察者是可用的。 主线程调用dealloc操作会让Observer对象的引用计数减为0,这时对象会被释放掉。 后台线程发送一个通知,如果此时Observer还未被释放,则会向其转发消息,并执行回调方法。而如果在回调执行的过程中对象被释放了,就会出现上面的问题。 当然,上面这个例子是故意而为之,但不排除在实际编码中会遇到类似的问题。虽然NSNotificationCenter是线程安全的,但并不意味着我们在使用时就可以保证线程安全的,如果稍不注意,还是会出现线程问题。 那我们该怎么做呢?这里有一些好的建议: 尽量在一个线程中处理通知相关的操作,大部分情况下,这样做都能确保通知的正常工作。不过,我们无法确定到底会在哪个线程中调用dealloc方法,所以这一点还是比较困难。 注册监听都时,使用基于block的API。这样我们在block还要继续调用self的属性或方法,就可以通过weak-strong的方式来处理。具体大家可以改造下上面的代码试试是什么效果。 使用带有安全生命周期的对象,这一点对象单例对象来说再合适不过了,在应用的整个生命周期都不会被释放。 使用代理。

㈥ ios如果1个方法里有多个网络请求 怎么依次施行

.新建一个网络请求工具类,负责整个项目中所有的Http网络请求

㈦ ios 多次请求多线程怎么处理

如何处理多个网络请求的并发的情况
一、概念
1.并发 当有多个线程在操作时,如果系统只有一个CPU,则它根本不可能真正同时进行一个以上的线程,它只能把CPU运行时间划分成若干个时间段,再将时间 段分配给各个线程执行,在一个时间段的线程代码运行时,其它线程处于挂起状。.这种方式我们称之为并发(Concurrent)。
2.并行 当系统有一个以上CPU时,则线程的操作有可能非并发。当一个CPU执行一个线程时,另一个CPU可以执行另一个线程,两个线程互不抢占CPU资源,可以同时进行,这种方式我们称之为并行(Parallel)。
3.区别 并发和并行是即相似又有区别的两个概念,并行是指两个或者多个事件在同一时刻发生;而并发是指两个或多个事件在同一时间间隔内发生。
举个栗子
1).并发 一个送外卖的A需要把两份外卖分别送到两个客户B和C手里。 A必须先送完B外卖才能接着送C的。这就是并发
2).并行 客户C 分别从饿了么和美团订了一共两份外卖。那么外卖员A和外卖员B需要把外卖一同送到客户C手里。 这就是并行
在iOS中,经常可以看见有这样的需求,就是一个方法要等另外一个方法执行完毕再做相对应的处理,比如说一些网络请求,需要根据上一个请求的返回值做相对应的处理再执行第二个请求,所以我们不能让两个请求同时去请求网络。下面就记录以下通过GCD和NSOperationQueue来控制并发。
二、代码部分(GCD)
dispatch_semaphore 信号量

㈧ iOS开发中网络请求的代码放到哪儿比较好

一般网络请求的代码都封装到一个工具类中。MVC的话就放到model层中

㈨ ios进行网络请求是开辟子线程吗

是的。因为直接在主线程里进行网络请求会阻塞主线程,所有开辟子线程进行网络请求,请求完成后返回主线程刷新UI

㈩ ios 异步网络请求 算是多线程吗

换成我们服务器的接口 查看原帖>>

阅读全文

与ios网络请求发生在哪个线程相关的资料

热点内容
智慧沃家路由器找不到网络 浏览:634
为什么房间有信号网络很差 浏览:523
谁的网络信号强 浏览:858
什么是施工网络图 浏览:918
泰国哪个运营商网络比较好 浏览:413
用什么方法传送网络成本低 浏览:622
严禁个人私自搭设无线网络 浏览:831
路由器网络层的关键功能是什么 浏览:698
网络正常电脑无法连接到internet 浏览:883
赢佳网络是什么意思 浏览:31
计算机路由器网络更新 浏览:444
qq飞车夸克网络异常 浏览:225
广东wifi网络设备 浏览:849
移动网络大师破限速 浏览:5
开共享网络费电还是自己手机 浏览:24
研究无线网络安全的意义 浏览:841
共建共享清朗网络的话语 浏览:384
xp设置网络连接 浏览:398
网络连接失败显示感叹号 浏览:215
中国网络安全技术有哪些 浏览:284

友情链接