【问题标题】:How to retrieve Node properties from Swift using Theo and Neo4j Database?如何使用 Theo 和 Neo4j 数据库从 Swift 中检索节点属性?
【发布时间】:2018-01-12 23:43:05
【问题描述】:

下面是我的 Swift Xcode 项目中的代码 sn-p。我目前正在使用 Swift 在 Xcode 中开发一个应用程序,并且我正在尝试使用 Neo4j 作为后端来存储所有数据等。

在使用 Swift 和 Neo4j 时,唯一的选择是使用 Theo 来连接 Neo4j 数据库并与之通信。您可以直接进行 Cypher 查询,但事实证明,当我想从 Neo4j 数据库中获取数据并将其本地存储在 Swift 中的变量或数组中时,这非常困难。

我的代码,请记住,这是直接取自 Neo4j Theo Swift 示例包,您可以在此处找到 - https://github.com/GraphStory/neo4j-ios

import UIKit
import Theo
import PackStream

class QueryViewController: UIViewController {

    var connectionConfig: ConnectionConfig?
    @IBOutlet weak var outputTextView: UITextView?

    //All of the User Interface Outlets, Buttons etc.
    @IBOutlet weak var createNodeButton: UIButton?
    @IBOutlet weak var fetchNodeButton: UIButton?
    @IBOutlet weak var runCypherButton: UIButton?
    @IBOutlet weak var runTransactionButton: UIButton?
    @IBOutlet weak var clientNameField: UITextField!
    @IBOutlet weak var clientYearBorn: UITextField!
    @IBOutlet weak var nodeInfo: UILabel!

    var query: String!
    var clientArray: [String] = []

    var clientID : Int = 0
    var clientName: String!
    var yearBorn: String!

    private var theo: BoltClient?
    private var lastNodeId: UInt64 = 1

    override func viewDidLoad() {
        super.viewDidLoad()

        disableButtons()
        if let config = connectionConfig {
            do {
                self.theo = try BoltClient(
                    hostname: config.host,
                    port: config.port,
                    username: config.username,
                    password: config.password,
                    encrypted: true)
            } catch {
                DispatchQueue.main.async { [weak self] in
                    self?.outputTextView?.text = "Failed during connection configuration"
                }
                return
            }

            guard let theo = self.theo else { return }

            log("Connecting...")

            let result = theo.connectSync()
            switch result {
            case .failure(_):
                log("Error while connecting")
            case .success(_):
                let result = theo.executeCypherSync("MATCH (n:ImpossibleNode) RETURN count(n) AS n")
                switch result {
                case let .failure(error):
                    log("Error while connecting: \(error)")
                case .success(_):
                    log("Connected")
                    DispatchQueue.main.async { [weak self] in
                        self?.enableButtons()
                    }
                }
            }

        } else {
            outputTextView?.text = "Missing connection configuration"
        }
    }

    private func enableButtons() {
        createNodeButton?.isEnabled = true
        fetchNodeButton?.isEnabled = true
        runCypherButton?.isEnabled = true
        runTransactionButton?.isEnabled = true
    }

    private func disableButtons() {
        createNodeButton?.isEnabled = false
        fetchNodeButton?.isEnabled = false
        runCypherButton?.isEnabled = false
        runTransactionButton?.isEnabled = false
    }

    //Here we can create a Node to add to the Neo4j Database.
    @IBAction func createNodeTapped(_ sender: UIButton) {

        guard let theo = self.theo else {
            log("Client not initialized yet")
            return
        }

    //Here we define some variables we wish to store within the node as Properties.
         clientName = clientNameField?.text
         yearBorn = clientYearBorn.text
         clientID = Int(lastNodeId)

         self.lastNodeId += 1

        let node = Node(label: "Client", properties:["id": clientID, "name": clientName, "yearBorn": yearBorn])

        let result = theo.createAndReturnNodeSync(node: node)
        switch result {
        case let .failure(error):
            log("Error while creating node: \(error)")
        case let .success(responseNode):
            log("Successfully created node: \(responseNode)")
            lastNodeId = responseNode.id!

            clientName = clientNameField?.text
            print("Added", clientName!, "to the Nexus")
            print(clientID)

        }
    }

    @IBAction func fetchNodeTapped(_ sender: UIButton) {

        guard let theo = self.theo else {
            log("Client not initialized yet")
            return
        }

        theo.nodeBy(id: lastNodeId) { result in
            switch result {
            case let .failure(error):
                self.log("Error while reading fetched node with ID '\(self.lastNodeId)': \(error)")
            case let .success(responseNode):
                if let responseNode = responseNode {
                    self.log("Fetched node with ID \(self.lastNodeId): \(responseNode)")

                    //Here I am attempting to grab all the nodes labeled "Client" with the property name "Mr Davies"

                    let results = theo.executeCypherSync("MATCH (c:Client{name: 'Mr Davies'}) RETURN (c)")

                    //Here I am appending the results from the Cypher Query above to an array - clientArray
                    self.clientArray.append((results.value?.nodes.description)!)

                    //Finally here I am printing to the console the client array.
                    print(self.clientArray)
                } else {
                    self.log("Could not find node with ID \(self.lastNodeId)")
                }
            }
        }
    }

    func log(_ string: String) {
        print(string)
        DispatchQueue.main.async {
            let text = self.outputTextView?.text ?? ""
            if text == "" {
                self.outputTextView?.text = string
            } else {
                self.outputTextView?.text = "\(string)\n\n\(text)"

            }
        }
    }

    @IBAction func runCypherTapped(_ sender: UIButton) {

        guard let theo = self.theo else {
            log("Client not initialized yet")
            return
        }

        let result = theo.executeCypherSync("MATCH (c:Client) RETURN count(c) AS num")
        switch result {
        case let .failure(error):
            log("Error while getting cypher results: \(error)")
        case let .success(queryResult):
            if let intNum = queryResult.rows[0]["num"] as? UInt64 {
                log("Asked via Cypher how many nodes there are with label Client. Answer: \(intNum)")

            } else {
                log("Got unexpected answer back")

            }
        }
    }

    @IBAction func runTransactionTapped(_ sender: UIButton) {

        do {
            try theo?.executeAsTransaction(transactionBlock: { (tx) in
                let query = "CREATE (C:Client { name: {name} } )"
                self.theo?.executeCypherSync(query, params: ["prop": "name"])
                self.theo?.executeCypherSync(query, params: ["prop": "yearborn"])

            })
        } catch {
            log("Error while executing transaction: \(error)")
            return
        }

        log("Transaction completed successfully")
    }
}

我使用这个应用程序的目的是获取所有标记为“客户端”的节点,然后将所述节点的属性存储到 Swift 中的 1 个或多个本地数组中。然后我可以在“fetchNodeTapped”函数中处理数据,我已经注释了代码。您可以看到我将数组内容打印到控制台,这就是我得到的:

获取 ID 为 260 的节点:Theo.Node ["[244: Theo.Node, 229: Theo.Node, 154:Theo.Node,230:Theo.Node,215:Theo.Node,226:Theo.Node,249: Theo.Node,177:Theo.Node,277:Theo.Node,214:Theo.Node,278: Theo.Node,222:Theo.Node,285:Theo.Node,206:Theo.Node,158: Theo.Node,258:Theo.Node,217:Theo.Node,167:Theo.Node,176: Theo.Node,223:Theo.Node,153:Theo.Node,168:Theo.Node,266: Theo.Node,228:Theo.Node,169:Theo.Node,220:Theo.Node,247: Theo.Node,150:Theo.Node,284:Theo.Node,216:Theo.Node,156: Theo.Node,272:Theo.Node,243:Theo.Node,255:Theo.Node,264: Theo.Node,268:Theo.Node,205:Theo.Node,273:Theo.Node,320: Theo.Node,175:Theo.Node,151:Theo.Node,257:Theo.Node,225: Theo.Node,172:Theo.Node,270:Theo.Node,274:Theo.Node,246: Theo.Node,224:Theo.Node,245:Theo.Node,283:Theo.Node,248: Theo.Node,170:Theo.Node,318:Theo.Node,160:Theo.Node,218: Theo.Node,152:Theo.Node,184:Theo.Node,256:Theo.Node,260: Theo.Node,231:Theo.Node,173:Theo.Node,171:Theo.Node,149: Theo.Node,157:Theo.Node,161:Theo.Node,221:Theo.Node,271: Theo.Node,227:Theo.Node,275:Theo.Node,276:Theo.Node,279: Theo.Node,155:Theo.Node,183:Theo.Node,219:Theo.Node,265: Theo.Node,267:Theo.Node,162:Theo.Node,250:Theo.Node,263: Theo.Node, 269: Theo.Node, 319: Theo.Node, 159: Theo.Node]"]

在这里您可以看到我的函数正在存储每个“Mr Davies”节点,当我将其打印到控制台时,它会打印出每个节点 ID“Theo.Node, 319: 例如。我想访问每个节点,这样我就可以从数据库中检索所有属性(名称、年份等),然后如上所述将这些属性存储在本地。

如果有人可以提供帮助,我们将不胜感激,如果您对我的问题还有任何疑问,请立即开火。

【问题讨论】:

    标签: swift xcode neo4j cypher


    【解决方案1】:

    我不知道我是否完全理解了你的问题,但是你可以使用keys() 函数来返回节点或关系的属性名称。例如:

    创建示例节点:

    CREATE (:Node {firstname : "Jon", lastname : "Doe", age : 20})
    

    然后:

    MATCH (node:Node)
    RETURN keys(node)
    

    输出将是:

    ╒══════════════════════════════╕
    │"keys(node)"                  │
    ╞══════════════════════════════╡
    │["firstname","age","lastname"]│
    └──────────────────────────────┘
    

    【讨论】:

    • 感谢您的快速响应,我已经使用这行代码进行了测试: let results = theo.executeCypherSync("MATCH (c:Client) RETURN keys (c)") print(results) 但是我收到“索引超出范围”错误,随后在模拟器中使应用程序崩溃。
    • @LukeIreton Index out of range 错误可能与 Cypher 执行无关。它应该与您的其他逻辑相关。
    • 好吧,我已经设法做到这一点 self.clientArray.append(results.value?.nodes.keys) print(self.clientArray) 但是现在我因转换错误而停止, "无法转换类型 'LazyMapCollection 的值?'到预期的参数类型“节点””对此有什么想法吗?我正在使用字符串数组来存储密钥。
    • @LukeIreton 很抱歉,我不是 Swift 程序员。
    • 好的,谢谢你的帮助,我认为使用键是正确的方向。
    猜你喜欢
    • 1970-01-01
    • 2016-11-15
    • 2023-01-30
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多