swift


session.dataTask not working Swift


I have a method which retrieves data from an API request:
let session = URLSession.shared
let url = URL(string: "http://eventregistry.org/json/article?conceptUri=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FAfghanistan&lang=eng&action=getArticles&articlesSortBy=date&resultType=articles")!
let task = session.dataTask(with: url) { (data, _, _) -> Void in
if let data = data {
print(JSON(data: data))
completion()
}
}
task.resume()
The link works perfectly when accessing through a browser, however when debugging line by line in XCode, none of the code within the let task = session.dataTask is executed and so the print is not printed within the log.
I am running through emulator and my laptop has internet, im just not sure why this is not working.
Does anyone understand why?
Try writing it this way. and also try to avoid the "Void"
First you need to make the urlRequest.
Example:
let urlRequest = URLRequest(url: URL(string: "Your url/API links here")!)
let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in {
//Your codes here
}
task.resume()
}
Hope it helps. If you need any further explanation, I will post a sample answer to retrieve the Registry Articles.

Related Links

How can I constantly update tableview per 'location changes'?
tableview not loading data correctly in cells Swift
How to delete sections in collection views
Pass selected row title back to main table view controller
AutoreleaseFrequency on DispatchQueue in Swift 3 beta 5
How can you use weak references in Swift generic data structure typed as a protocol?
plugin com.apple.share.Mail.compose invalidated (Cocoa-NSSharingServiceNameComposeEmail)
What should I convert my framework project to swift 2.3 or 3?
Swift when to use Performing Block Operations
MSMessageViewController deinit when switching to compact view
swift UInt32 0 or 0x0?
Swift: MCSession connecting, but never fully connects
How to make a video pause when a calayer appears
Performing functions with valueForKey
NSTextField updated randomly through delegate in Swift
How to adjust brightness with NSSlider for NSImage class via Swift?

Categories

HOME
arrays
jenkins-plugins
erlang
project-management
jar
firebase-app-indexing
jasmine
json-ld
hana
phonegap-cli
branch.io
snap.svg
gimp
google-plus
pca
ndis
nixos
gatsby
cosmicmind
riak-ts
pyyaml
google-openid
kadanes-algorithm
koa
go-cd
bootstrap-typeahead
philips-hue
python-textprocessing
croppic
branch
lync-2013
system.data.sqlite
hana-studio
positioning
xor
encase
graph-databases
spreedly
fabric-digits
passenger
initializer
parent
rule
intellij-idea-2016
sendgrid-templates
viewgroup
stereo-3d
excel-interop
powermta
php-ews
broadcast
goquery
chrome-remote-desktop
crystal-reports-8.5
instruction-set
tofixed
collapsingtoolbarlayout
visual-studio-templates
sqlexception
c64
icefaces
mix
webkit2
multifile-uploader
multilingual-app-toolkit
approval-tests
self-hosting
function-fitting
wikitext
zuora
android-snackbar
ipojo
harp
xaml-designer
subversion-edge
es2015
magento-1.12
kendo-mvvm
node-inspector
docopt
outline
code-testing
phpdocx
late-static-binding
document-database
.net-cf-3.5
asp.net-mvc-scaffolding
dig
apache2.2
dwolla
qss
pascals-triangle
xml-libxml
caliper
macruby
expression-evaluation
parameterization
visual-web-gui
viewdidload
tcxgrid
easygui
loadui
usability-testing
data-execution-prevention

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App