Objectively
Object oriented framework for C.
Loading...
Searching...
No Matches
URLSession Struct Reference

#include <URLSession.h>

Overview

A management context for loading resources via URLs.

Remarks
Tasks do not retain their session. Deallocating a URLSession cancels its tasks, and a task MUST NOT be used after its session is deallocated.

Definition at line 59 of file URLSession.h.

Inheritance diagram for URLSession:
Object

Properties

URLSessionConfiguration * configuration
 The session configuration.
 
Object object
 The superclass.
 
- Properties inherited from Object
Class * clazz
 Every instance of Object begins with a pointer to its Class.
 
unsigned int magic
 A header to allow introspection of Object types.
 

Methods

Class * _URLSession (void)
 The URLSession archetype.
 
URLSessionDataTask * dataTaskWithRequest (URLSession *, URLRequest *, URLSessionTaskCompletion)
 Creates a URLSessionDataTask for the given URLRequest.
 
URLSessionDataTask * dataTaskWithURL (URLSession *, URL *, URLSessionTaskCompletion)
 Creates a URLSessionDataTask for the given URL.
 
URLSessionDownloadTask * downloadTaskWithRequest (URLSession *, URLRequest *, URLSessionTaskCompletion)
 Creates a URLSessionDownloadTask for the given URLRequest.
 
URLSessionDownloadTask * downloadTaskWithURL (URLSession *, URL *, URLSessionTaskCompletion)
 Creates a URLSessionDownloadTask for the given URL.
 
URLSession * init (URLSession *)
 Initializes this URLSession with a default configuration.
 
URLSession * initWithConfiguration (URLSession *, URLSessionConfiguration *)
 Initializes this URLSession with the given configuration.
 
void invalidateAndCancel (URLSession *)
 Invalidates this URLSession and cancels all pending tasks.
 
URLSession * sharedInstance ()
 
Array * tasks (const URLSession *)
 
URLSessionUploadTask * uploadTaskWithRequest (URLSession *, URLRequest *, URLSessionTaskCompletion)
 Creates a URLSessionUploadTask for the given URLRequest.
 
- Methods inherited from Object
Class * _Object (void)
 The Object archetype.
 
Object * copy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
String * description (const Object *self)
 
int hash (const Object *self)
 
Object * init (Object *self)
 Initializes this Object.
 
bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object.
 
bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership.
 

Protected Attributes

URLSessionInterface * interface [0]
 The interface type.
 
- Protected Attributes inherited from Object
ObjectInterface * interface [0]
 The interface type.
 

Property Details

◆ configuration

URLSessionConfiguration* URLSession::configuration

The session configuration.

Definition at line 81 of file URLSession.h.

◆ interface

URLSessionInterface* URLSession::interface[0]
protected

The interface type.

Definition at line 70 of file URLSession.h.

◆ object

Object URLSession::object

The superclass.

Definition at line 64 of file URLSession.h.

Method Details

◆ _URLSession()

Class * _URLSession ( void  )

The URLSession archetype.

Returns
The URLSession Class.

Definition at line 496 of file URLSession.c.

496 {
497 static Class *clazz;
498 static Once once;
499
500 do_once(&once, {
501 clazz = _initialize(&(const ClassDef) {
502 .name = "URLSession",
503 .superclass = _Object(),
504 .instanceSize = sizeof(URLSession),
505 .interfaceSize = sizeof(URLSessionInterface),
506 .initialize = initialize,
507 .destroy = destroy,
508 });
509 });
510
511 return clazz;
512}
static void initialize(Class *clazz)
Definition Array.c:710
static void destroy(Class *clazz)
Definition Boole.c:102
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:151
long Once
The Once type.
Definition Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition Class.h:41
The runtime representation of a Class.
Definition Class.h:90
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition Object.h:55
Class * _Object(void)
The Object archetype.
Definition Object.c:136
A management context for loading resources via URLs.
Definition URLSession.h:59

◆ dataTaskWithRequest()

URLSessionDataTask * dataTaskWithRequest ( URLSession *  self,
URLRequest *  request,
URLSessionTaskCompletion  completion 
)

Creates a URLSessionDataTask for the given URLRequest.

Parameters
selfThe URLSession.
requestThe URLRequest to perform.
completionThe completion handler.
Returns
The URLSessionDataTask, or NULL if this URLSession is invalidated.

Definition at line 85 of file URLSession.c.

85 {
86
88 initWithRequestInSession, request, self, completion);
89 if (task) {
90
91 bool isInvalidated;
92
93 synchronized(self->condition, {
94 isInvalidated = $(self->thread, isCancelled);
95 });
96
97 if (isInvalidated) {
98 return release(task);
99 }
100
101 URLCache *cache = self->configuration->urlCache;
102 if (cache) {
104 if (task->cachedResponse) {
105 return task;
106 }
107 }
108
109 synchronized(self->condition, {
110 isInvalidated = $(self->thread, isCancelled);
111 if (isInvalidated == false) {
112 $(self->tasks, addObject, task);
113 $(self->condition, signal);
114 }
115 });
116
117 if (isInvalidated) {
118 task = release(task);
119 }
120 }
121
122 return task;
123}
static void addObject(Array *self, const ident obj)
Definition Array.c:181
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:356
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:226
static bool isCancelled(const Operation *self)
Definition Operation.c:140
static int request(RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, const char **headers, Data **out_data)
Definition RESTClient.c:62
static URLCachedResponse * cachedResponseForRequest(URLCache *self, const URLRequest *request)
Definition URLCache.c:268
static URLSessionTask * initWithRequestInSession(URLSessionTask *self, struct URLRequest *request, struct URLSession *session, URLSessionTaskCompletion completion)
A cache for HTTP responses.
Definition URLCache.h:44
Use data tasks to send and receive Data in-memory.
URLCachedResponse * cachedResponse
A cached response, if this task was fulfilled from URLCache.
URL session tasks are handles to pending URL operations.

◆ dataTaskWithURL()

URLSessionDataTask * dataTaskWithURL ( URLSession *  self,
URL *  url,
URLSessionTaskCompletion  completion 
)

Creates a URLSessionDataTask for the given URL.

Parameters
selfThe URLSession.
urlThe URL to GET.
completionThe completion handler.
Returns
The URLSessionDataTask, or NULL if this URLSession is invalidated.

Definition at line 129 of file URLSession.c.

129 {
130
132 request->httpMethod = HTTP_GET;
133
134 URLSessionDataTask *task = $(self, dataTaskWithRequest, request, completion);
135
137
138 return task;
139}
static URLRequest * initWithURL(URLRequest *self, URL *url)
Definition URLRequest.c:150
@ HTTP_GET
Definition URLRequest.h:46
A protocol-agnostic abstraction for requesting resources via URLs.
Definition URLRequest.h:58
URLSessionDataTask * dataTaskWithRequest(URLSession *, URLRequest *, URLSessionTaskCompletion)
Creates a URLSessionDataTask for the given URLRequest.
Definition URLSession.c:85

◆ downloadTaskWithRequest()

URLSessionDownloadTask * downloadTaskWithRequest ( URLSession *  self,
URLRequest *  request,
URLSessionTaskCompletion  completion 
)

Creates a URLSessionDownloadTask for the given URLRequest.

Parameters
selfThe URLSession.
requestThe URLRequest to perform.
completionThe completion handler.
Returns
The URLSessionDownloadTask, or NULL if this URLSession is invalidated.

Definition at line 145 of file URLSession.c.

145 {
146
147 return taskWithRequest(self, alloc(URLSessionDownloadTask), request, completion);
148}
static ident taskWithRequest(URLSession *self, ident task, URLRequest *request, URLSessionTaskCompletion completion)
URLSessionTask factory function.
Definition URLSession.c:58
Use download tasks to save remote resources to file.

◆ downloadTaskWithURL()

URLSessionDownloadTask * downloadTaskWithURL ( URLSession *  self,
URL *  url,
URLSessionTaskCompletion  completion 
)

Creates a URLSessionDownloadTask for the given URL.

Parameters
selfThe URLSession.
urlThe URL to GET.
completionThe completion handler.
Returns
The URLSessionDownloadTask, or NULL if this URLSession is invalidated.

Definition at line 154 of file URLSession.c.

154 {
155
157
158 URLSessionDownloadTask *task = $(self, downloadTaskWithRequest, request, completion);
159
161
162 return task;
163}
URLSessionDownloadTask * downloadTaskWithRequest(URLSession *, URLRequest *, URLSessionTaskCompletion)
Creates a URLSessionDownloadTask for the given URLRequest.
Definition URLSession.c:145

◆ init()

URLSession * init ( URLSession *  self)

Initializes this URLSession with a default configuration.

Parameters
selfThe URLSession.
Returns
The initialized URLSession, or NULL on error.

Definition at line 169 of file URLSession.c.

169 {
170
172
173 self = $(self, initWithConfiguration, configuration);
174
176
177 return self;
178}
Configuration bundle for URLSession.
URLSessionConfiguration * configuration
The session configuration.
Definition URLSession.h:81
URLSession * init(URLSession *)
Initializes this URLSession with a default configuration.
Definition URLSession.c:169
URLSession * initWithConfiguration(URLSession *, URLSessionConfiguration *)
Initializes this URLSession with the given configuration.
Definition URLSession.c:368

◆ initWithConfiguration()

URLSession * initWithConfiguration ( URLSession *  self,
URLSessionConfiguration *  configuration 
)

Initializes this URLSession with the given configuration.

Parameters
selfThe URLSession.
configurationThe URLSessionConfiguration.
Returns
The initialized URLSession, or NULL on error.

Definition at line 368 of file URLSession.c.

368 {
369
370 assert(configuration);
371
372 self = (URLSession *) super(Object, self, init);
373 if (self) {
375
376 self->condition = $(alloc(Condition), init);
377 self->tasks = $(alloc(Array), init);
378 self->thread = $(alloc(Thread), initWithFunction, run, self);
379
380 $(self->thread, start);
381 }
382
383 return self;
384}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:372
#define super(type, obj, method,...)
static Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Definition Operation.c:125
static void start(Operation *self)
Definition Operation.c:185
static ident run(Thread *thread)
ThreadFunction for the OperationQueue Threads.
Arrays.
Definition Array.h:56
POSIX Threads conditional variables.
Definition Condition.h:44
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
POSIX Threads.
Definition Thread.h:53

◆ invalidateAndCancel()

void invalidateAndCancel ( URLSession *  self)

Invalidates this URLSession and cancels all pending tasks.

Parameters
selfThe URLSession.
Remarks
Tasks that URLSessionTask::execute is running on other threads are interrupted too. After this call, the task factories return NULL. This method returns without waiting for the cancelled tasks. Deallocating the URLSession invalidates it, then waits for its Thread, which finishes once every task has completed or been cancelled.

Definition at line 390 of file URLSession.c.

390 {
391
392 bool isInvalidated;
393
394 synchronized(self->condition, {
395 isInvalidated = $(self->thread, isCancelled);
396 if (isInvalidated == false) {
397 $(self->thread, cancel);
398 $(self->condition, signal);
399 }
400 });
401
402 if (isInvalidated) {
403 return;
404 }
405
406 Array *tasks = $(self, tasks);
407
408 for (size_t i = 0; i < tasks->count; i++) {
409
410 URLSessionTask *task = $(tasks, objectAtIndex, i);
411 $(task, cancel);
412 }
413
414 release(tasks);
415}
static ident objectAtIndex(const Array *self, size_t index)
Definition Array.c:578
static void cancel(Operation *self)
Definition Operation.c:73
static Array * tasks(const URLSession *self)
Definition URLSession.c:438
size_t count
The count of elements.
Definition Array.h:72

◆ sharedInstance()

URLSession * sharedInstance ( void  )
Returns
The shared URLSession instance.

Definition at line 212 of file Log.c.

212 {
213
214 static Once once;
215
216 do_once(&once, {
218 });
219
220 return _sharedInstance;
221}
static Log * _sharedInstance
Definition Log.c:206
A Log4J-inspired log appender.
Definition Log.h:61

◆ tasks()

Array * tasks ( const URLSession *  self)
Parameters
selfThe URLSession.
Returns
An instantaneous copy of this URLSession's URLSessionTasks.

Definition at line 438 of file URLSession.c.

438 {
439
440 Array *array;
441
442 synchronized(self->condition, {
443 array = $$(Array, arrayWithArray, (Array *) self->tasks);
444 });
445
446 return array;
447}
static Array * arrayWithArray(const Array *array)
Definition Array.c:243
static Array * array(void)
Definition Array.c:234

◆ uploadTaskWithRequest()

URLSessionUploadTask * uploadTaskWithRequest ( URLSession *  self,
URLRequest *  request,
URLSessionTaskCompletion  completion 
)

Creates a URLSessionUploadTask for the given URLRequest.

Parameters
selfThe URLSession.
requestThe URLRequest to perform.
completionThe completion handler.
Returns
The URLSessionUploadTask, or NULL if this URLSession is invalidated.

Definition at line 453 of file URLSession.c.

453 {
454
455 return taskWithRequest(self, alloc(URLSessionUploadTask), request, completion);
456}
Use upload tasks to send files directly from disk.

The documentation for this struct was generated from the following files: