Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

ios - How to pass an active URLSession with data task from one view controller to another view controller

I understand how to pass simple data (e.g. Structures, Ints, Strings, Floats) between one view controller to another through properties. However, I get stumped when passing an active URLsession from one view controller to another.

Scenario, I have a task that starts uploading potential (large size) content from one view controller and needs to move to another ViewController most likely before the URLsession.shared.dataTask is completed. However, I need the URLsession to remain persistent and update the app when it's completed. I have thought of using a singleton for this case and saw that singletons are not the best way to go about things, but should work. I am fine with making a separate completion handler to handle the task in the other ViewController, but I am not quite sure how to actually implement this via code. It would be really nice if someone could post an example.

For now, just so that is is working, I am doing a one time thing and putting the task in the AppDelegate file. This is not recommended, but for one task it will work for now.

Also, I am aware of the "container" method, I do not want to put it in a container view controller at this time because it is a part of an onboarding process. So, I would prefer to pass it at this moment.

I am unsure about:

Will a URLsession that is initiated in one ViewController continue to run on the background thread even if the ViewController it is initiated on is destroyed?

If the answer is yes to that question, how do I go about accessing the response to the call or hearing that it's completed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...