Connectors are Screenlet components that make server calls. Non-list Screenlets don’t require Connectors–they can make server calls in Interactors instead. Connectors, however, provide a layer of abstraction by making the server call outside the Interactor. This leaves the Interactor to instantiate the Connector and receive the server call’s results. List Screenlets exploit this architectural advantage by requiring Connectors.
First, you’ll create a folder for the Connector.
Creating Your Connector’s Folder
Follow these steps to create your Connector’s folder:
-
In the Finder, create the
Connector
folder inside your project’sGuestbookListScreenlet
folder. -
Drag and drop the
Connector
folder from the Finder into your Xcode project, under theGuestbookListScreenlet
folder. In the dialog that appears, select Copy items if needed, Create groups, and the Liferay Guestbook target. Then click Finish. TheConnector
folder now appears in your project.Figure 1: The new `Connector` folder should be inside the Screenlet's folder.
Now you’re ready to create the Connector.
Creating the Connector
List Screenlet Connectors must extend
the PaginationLiferayConnector
class,
which Liferay Screens provides to enable most of the functionality required by
list Screenlet Connectors. Extending this class lets you focus on the
functionality unique to your Connector. Your list Screenlet’s Connector class
must contain any properties it needs to make the server call, and an initializer
that sets them. To support pagination, the initializer must also contain the
following arguments, which you’ll pass to the superclass initializer:
startRow
: The number representing the page’s first row.endRow
: The number representing the page’s last row.computeRowCount
: Whether to call the Connector’sdoAddRowCountServiceCall
method (you’ll learn about this method shortly).
Follow these steps to create Guestbook List Screenlet’s Connector:
-
In the Project navigator, right-click the
Connector
folder and select New File. In the dialog that appears, fill out each screen as follows:- Select iOS → Source → Cocoa Touch Class, and click Next.
- Name the class
GuestbookListPageLiferayConnector
, set it to extendPaginationLiferayConnector
, select Swift for the language, and click Next. - Make sure the
Connector
folder and group is selected, as well as the Liferay Guestbook target (these should be selected by default). Click Create.
-
In the new class, import
LiferayScreens
and add a publicInt64
constant calledgroupId
. This constant holds the ID of the site the Connector retrieves guestbooks from. Your Connector should now look like this:import UIKit import LiferayScreens class GuestbookListPageLiferayConnector: PaginationLiferayConnector { public let groupId: Int64 }
-
Create an initializer that takes the arguments
startRow
,endRow
,computeRowCount
, andgroupId
. In this initializer, set thegroupId
constant to the corresponding argument, then call the superclass initializer with the remaining arguments. Add the initializer as follows:public init(startRow: Int, endRow: Int, computeRowCount: Bool, groupId: Int64) { self.groupId = groupId super.init(startRow: startRow, endRow: endRow, computeRowCount: computeRowCount) }
-
Override the
doAddPageRowsServiceCall
method to make the server call that retrieves guestbooks from the portlet. This method must call the Guestbook SDK service methodgetGuestbooksWithGroupId
, which retrieves guestbooks. To do this, you must first create aLRGuestbookService_v7
instance from the session. Then call the service’sgetGuestbooksWithGroupId
method withgroupId
,startRow
, andendRow
:public override func doAddPageRowsServiceCall(session: LRBatchSession, startRow: Int, endRow: Int, obc: LRJSONObjectWrapper?) { let service = LRGuestbookService_v7(session: session) do { try service!.getGuestbooksWithGroupId(groupId, start: Int32(startRow), end: Int32(endRow)) } catch { // ignore error: the service method returns nil because // the request is sent later, in batch } }
Note that you don’t need to do anything in the
catch
statement because the request is sent later, in batch. Thesession
typeLRBatchSession
handles this for you. You’ll receive the request’s results elsewhere, once the request completes. -
Override the
doAddRowCountServiceCall
method to make the server call that retrieves the total number of guestbooks from the portlet. This enables pagination. This method must call the Guestbook SDK service methodgetGuestbooksCount
, which retrieves the total number of guestbooks. To do this, you must first create aLRGuestbookService_v7
service instance from the session. Then call the service’sgetGuestbooksCount
method withgroupId
:override public func doAddRowCountServiceCall(session: LRBatchSession) { let service = LRGuestbookService_v7(session: session) do { try service!.getGuestbooksCount(withGroupId: groupId) } catch { // ignore error: the service method returns nil because // the request is sent later, in batch } }
As in the previous step, you don’t need to do anything in the
catch
statement.
Awesome! Your Connector is finished. Now you’re ready to create the Interactor.