Use Alamofire in iOS Apps | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/9261385-alamofire/lessons/1

Hello,

I am getting this error when I run swift run:

/TILApp/Sources/App/configure.swift:61:65: error: reference to member ‘sqlite’ cannot be resolved without a contextual type
migrations.add(model: AcronymCategoryPivot.self, database: .sqlite)

Argh this is a bug in Swift 5.2 that broke a lot of things. If you edit Sources/App/configure.swift and replace the migrations.add(model:) lines with the following:

migrations.add(model: User.self, database: DatabaseIdentifier<User.Database>.sqlite)
migrations.add(model: Acronym.self, database: DatabaseIdentifier<Acronym.Database>.sqlite)
migrations.add(model: TILCategory.self, database: DatabaseIdentifier<TILCategory.Database>.sqlite)
migrations.add(model: AcronymCategoryPivot.self, database: DatabaseIdentifier<AcronymCategoryPivot.Database>.sqlite)

Then it will work, I’ll get updated materials pushed out ASAP

1 Like

Thanks you for your help!

I was able to run the server after making above changes but now getting an error message when I try to open localhost:8080 on my browser.

[ ERROR ] Abort.404: Not Found (ApplicationResponder.swift:50)

Yeah there’s no route registered for /, so if you do /api/users instead you’ll get an array of users. (You also might see a 404 in the browser as it tries to get a favicon, but you can ignore these)

1 Like

Just to add, the videos that this affects (episodes 1-4) should now all be updated with code that builds

1 Like

Hello,
I need some explanation regarding two points.

  1. What is the importance of using Session while making requests?

  2. The completion handler of responseDecodable runs on main thread by default. So why do we have to explicitly run the code on main thread in the success callback? Like in this case:

     case .success(let categories):
         DispatchQueue.main.async { [weak self] in
           guard let self = self else { return }
           self.categories = categories
           self.tableView.reloadData()
         }

@hamna

  1. Session is used to store all the request configuration stuff. When using URLSession you normally configure it will some options, or use the default options. This is just a wrapper around that, and allows us to change it as we need going forward (for instance when doing SSL pinning).

  2. It’s good practice to always jump onto the main thread when changing anything to do with the UI. In this instance, we may be on the main thread when hitting that, but there could be a future instance where we aren’t and call reload on a background thread etc

Hope that helps!

1 Like

<1> HTTP load failed, 0/0 bytes (error code: -1004 [1:61])
I run simulator but acronyms data is nil
what should I do?

What does the Vapor app console say and is the Vapor app running on the same machine? 1004 means it can’t connect to the host

In file included from /Users/gangi/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/swift-nio-ssl/Sources/CNIOOpenSSL/helpers.c:14:
/Users/gangi/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/swift-nio-ssl/Sources/CNIOOpenSSL/include/c_nio_openssl.h:17:10: fatal error: ‘openssl/conf.h’ file not found
In file included from /Users/gangi/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/crypto/Sources/CCryptoOpenSSL/shim.c:1:
/Users/gangi/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/crypto/Sources/CCryptoOpenSSL/include/c_crypto_openssl.h:4:10: fatal error: ‘openssl/conf.h’ file not found
In file included from /Users/gangi/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/swift-nio-ssl/Sources/CNIOOpenSSL/shims.c:16:
/Users/gangi/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/swift-nio-ssl/Sources/CNIOOpenSSL/include/10: fatal error: ‘openssl/conf.h’ file not found
#include <openssl/conf.h>
^~~~~~~~~~~~~~~~
#include <openssl/conf.h>
^~~~~~~~~~~~~~~~
#include <openssl/conf.h>
^~~~~~~~~~~~~~~~
1 error generated.
1 error generated.
[3/20] Compiling CNIOSHA1 c_nio_sha1.c
error: fatalError

Ah the Alamofire example still uses Vapor 3 IIRC which contains some breaking changes. Try running brew install pkg-config libressl - that should install the necessary libraries to get it to build from the command line

Hi,
I’m getting this error when I run “swift run”:

‘swift-nio-ssl-support’: warning: ignoring declared target(s) ‘swift-nio-ssl-support’ in the system package
‘swift-nio-zlib-support’: warning: ignoring declared target(s) ‘swift-nio-zlib-support’ in the system package
Building for debugging…
/Users/ralf/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/swift-nio/Sources/NIO/ContiguousCollection.swift:21:1: error: type ‘StaticString’ does not conform to protocol ‘Collection’
extension StaticString: Collection {
^
/Users/ralf/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/swift-nio/Sources/NIO/ContiguousCollection.swift:21:1: error: unavailable subscript ‘subscript(:)’ was used to satisfy a requirement of protocol ‘Collection’
extension StaticString: Collection {
^
Swift.Collection:3:12: note: 'subscript(
:)’ declared here
public subscript(bounds: Range<Self.Index>) → Self.SubSequence { get }
^
Swift.Collection:12:5: note: requirement ‘subscript(:)’ declared here
subscript(bounds: Range<Self.Index>) → Self.SubSequence { get }
^
/Users/ralf/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/swift-nio/Sources/NIO/ContiguousCollection.swift:21:1: error: type ‘StaticString’ does not conform to protocol ‘Collection’
extension StaticString: Collection {
^
/Users/ralf/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/swift-nio/Sources/NIO/ContiguousCollection.swift:21:1: error: unavailable subscript 'subscript(
:)’ was used to satisfy a requirement of protocol ‘Collection’
extension StaticString: Collection {
^
Swift.Collection:3:12: note: ‘subscript(:)’ declared here
public subscript(bounds: Range<Self.Index>) → Self.SubSequence { get }
^
Swift.Collection:12:5: note: requirement 'subscript(
:)’ declared here
subscript(bounds: Range<Self.Index>) → Self.SubSequence { get }
^
/Users/ralf/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/swift-nio/Sources/NIO/Selector.swift:60:16: warning: static property ‘_none’ produces an empty option set
static let _none = SelectorEventSet(rawValue: 0)
^
/Users/ralf/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/swift-nio/Sources/NIO/Selector.swift:60:16: note: use [] to silence this warning
static let _none = SelectorEventSet(rawValue: 0)
^ ~~~~~~~~~~~~~
([])
/Users/ralf/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/swift-nio/Sources/NIO/Selector.swift:89:16: warning: static property ‘_none’ produces an empty option set
static let _none = KQueueEventFilterSet(rawValue: 0)
^
/Users/ralf/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/swift-nio/Sources/NIO/Selector.swift:89:16: note: use [] to silence this warning
static let _none = KQueueEventFilterSet(rawValue: 0)
^ ~~~~~~~~~~~~~
([])
/Users/ralf/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/swift-nio/Sources/NIO/Selector.swift:112:16: warning: static property ‘_none’ produces an empty option set
static let _none = EpollFilterSet(rawValue: 0)
^
/Users/ralf/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/swift-nio/Sources/NIO/Selector.swift:112:16: note: use [] to silence this warning
static let _none = EpollFilterSet(rawValue: 0)
^ ~~~~~~~~~~~~~
([])
[2/3] Emitting module NIO

Run swift package update and then try swift run again

Thanks for your quick reply but it still doesn’t work.

“swift package update” rises that warnings:
‘swift-nio-ssl-support’: warning: ignoring declared target(s) ‘swift-nio-ssl-support’ in the system package
‘swift-nio-zlib-support’: warning: ignoring declared target(s) ‘swift-nio-zlib-support’ in the system package

“swift run” outputs a lot but server is not running. Hope that extracted dump helps:

Generic signature <τ_0_0 where τ_0_0 : SQLConnectable, τ_0_0.Connection.Query.AlterTable == SQLiteAlterTable> exceeds maximum completion depth

Rewrite system: {

  • [SQLConnectable].[SQLConnectable] => [SQLConnectable] [permanent]

  • [SQLConnectable].Connection => [SQLConnectable:Connection] [permanent]

  • [SQLConnection].[SQLConnection] => [SQLConnection] [permanent]

[…]

  1. Apple Swift version 5.6 (swiftlang-5.6.0.323.62 clang-1316.0.20.8)
  2. Compiling with effective version 4.1.50
  3. While evaluating request TypeCheckSourceFileRequest(source_file “/Users/ralf/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/sqlite/Sources/SQLite/SQL/SQLiteAlterTableBuilder.swift”)
  4. While type-checking extension of SQLAlterTableBuilder (at /Users/ralf/Downloads/P01 Use Alamofire/starter/TILApp/.build/checkouts/sqlite/Sources/SQLite/SQL/SQLiteAlterTableBuilder.swift:1:1)
  5. While evaluating request RequirementRequest(extension of SQLAlterTableBuilder, 0, interface)
  6. While evaluating request ResolveTypeRequest(while resolving type , Connectable.Connection.Query.AlterTable, (null))
  7. While building rewrite system for generic signature <τ_0_0 where τ_0_0 : SQLConnectable, τ_0_0.Connection.Query.AlterTable == SQLiteAlterTable>
    Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var LLVM_SYMBOLIZER_PATH to point to it):
    0 swift-frontend 0x0000000104872f88 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
    1 swift-frontend 0x0000000104871f9c llvm::sys::RunSignalHandlers() + 112
    2 swift-frontend 0x0000000104873618 SignalHandler(int) + 344
    3 libsystem_platform.dylib 0x000000019a7194c4 _sigtramp + 56
    4 libsystem_pthread.dylib 0x000000019a701ee0 pthread_kill + 288
    5 libsystem_c.dylib 0x000000019a63c340 abort + 168
    6 swift-frontend 0x00000001013a2158 swift::rewriting::RuleBuilder::~RuleBuilder() + 0
    7 swift-frontend 0x00000001013a1574 swift::rewriting::RequirementMachine::computeCompletion(swift::rewriting::RewriteSystem::ValidityPolicy) + 84
    8 swift-frontend 0x00000001013a1448 swift::rewriting::RequirementMachine::initWithGenericSignature(swift::CanGenericSignature) + 892
    9 swift-frontend 0x00000001013a849c swift::rewriting::RewriteContext::getRequirementMachine(swift::CanGenericSignature) + 240
    10 swift-frontend 0x00000001012cecc8 swift::GenericSignatureImpl::lookupNestedType(swift::Type, swift::Identifier) const + 544
    11 swift-frontend 0x0000000100fa59a0 (anonymous namespace)::TypeResolver::resolveIdentifierType(swift::IdentTypeRepr*, swift::TypeResolutionOptions) + 1716
    12 swift-frontend 0x0000000100fa2008 (anonymous namespace)::TypeResolver::resolveType(swift::TypeRepr*, swift::TypeResolutionOptions) + 228
    13 swift-frontend 0x0000000100fa1d8c swift::ResolveTypeRequest::evaluate(swift::Evaluator&, swift::TypeResolution const*, swift::TypeRepr*, swift::GenericParamList*) const + 104
    14 swift-frontend 0x0000000100fb0f2c llvm::Expectedswift::ResolveTypeRequest::OutputType swift::Evaluator::getResultUncachedswift::ResolveTypeRequest(swift::ResolveTypeRequest const&) + 484
    15 swift-frontend 0x0000000100fa1c9c swift::TypeResolution::resolveType(swift::TypeRepr*, swift::GenericParamList*) const + 96
    16 swift-frontend 0x0000000100f21bf0 swift::RequirementRequest::evaluate(swift::Evaluator&, swift::WhereClauseOwner, unsigned int, swift::TypeResolutionStage) const + 508
    17 swift-frontend 0x00000001013e1aa0 llvm::Expectedswift::RequirementRequest::OutputType swift::Evaluator::getResultUncachedswift::RequirementRequest(swift::RequirementRequest const&) + 1044
    18 swift-frontend 0x00000001013df524 swift::WhereClauseOwner::visitRequirements(swift::TypeResolutionStage, llvm::function_ref<bool (swift::Requirement, swift::RequirementRepr*)>) const && + 936
    19 swift-frontend 0x0000000100f01f84 checkGenericParams(swift::GenericContext*) + 292
    20 swift-frontend 0x0000000100efd350 swift::ASTVisitor<(anonymous namespace)::DeclChecker, void, void, void, void, void, void>::visit(swift::Decl*) + 15096
    21 swift-frontend 0x0000000100ef6a7c (anonymous namespace)::DeclChecker::visit(swift::Decl*) + 400
    22 swift-frontend 0x0000000100ef68e0 swift::TypeChecker::typeCheckDecl(swift::Decl*, bool) + 204
    23 swift-frontend 0x0000000100fb283c swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 540
    24 swift-frontend 0x0000000100fb55e0 llvm::Expectedswift::TypeCheckSourceFileRequest::OutputType swift::Evaluator::getResultUncachedswift::TypeCheckSourceFileRequest(swift::TypeCheckSourceFileRequest const&) + 564
    25 swift-frontend 0x0000000100fb25cc swift::performTypeChecking(swift::SourceFile&) + 120
    26 swift-frontend 0x00000001002875d0 swift::CompilerInstance::performSema() + 256
    27 swift-frontend 0x0000000100114348 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4072
    28 swift-frontend 0x00000001000dc130 swift::mainEntry(int, char const**) + 808
    29 dyld 0x0000000106f69088 start + 516

ralf@MBP-von-Ralf-2 TILApp %

Ok so it looks like an issues with SQLite on 5.6. Unfortunately Vapor 3 is EOL now. I’ll see if I can get some time to update the project to Vapor 4 but for now if you edit the docker-compose.yml file so it looks like

version: '3'
services:
  til-app:
    depends_on:
      - postgres
    build: .
    environment:
      - DATABASE_HOSTNAME=postgres
      - DATABASE_PORT=5432
    ports:
      - "8080"
  postgres:
    image: "postgres"
    environment:
      - POSTGRES_DB=vapor-test
      - POSTGRES_USER=vapor
      - POSTGRES_PASSWORD=password

Then run

docker-compose up

That should run the server in Docker for you and all work

Thank you for your answer. I changed docker-compose.yml as requested but I got an error during the building:

 => ERROR [internal] load metadata for docker.io/library/swift:4.2         7.9s
------
 > [internal] load metadata for docker.io/library/swift:4.2:
------
failed to solve with frontend dockerfile.v0: failed to create LLB definition: no match for platform in manifest

Could it be that I’m in trouble as I’m using a M1 MacBook instead an Intel one?

@ralfm yep it’s because it’s an M1 :upside_down_face:

I’ve built a Vapor 4 version of the starter project at GitHub - 0xTim/AlamofireServer

That should work instead

@0xtim That works! :smiley: Thanks a lot.

1 Like