【问题标题】:locationManager and data managinglocationManager 和数据管理
【发布时间】:2016-02-15 11:55:05
【问题描述】:

我正在编写一个应用程序来扫描 iBeacon 设备并使用 Alamofire 和 SwiftyJSON 从与信标相关的数据库数据中获取。我的问题是每秒扫描一次设备,我想将信标的实际表视图保持在范围内。例如,当我同时丢失几个信标时,数组索引超出边界错误。

    func locationManager(locationManager: KTKLocationManager!, didRangeBeacons beacons: [AnyObject]!) {
    let knownBeacons = beacons.filter{ $0.proximity != CLProximity.Unknown }
    let group = dispatch_group_create()

    dispatch_group_enter(group)
    deleteFromFoundBeacons(knownBeacons)
    dispatch_group_leave(group)

    dispatch_group_notify(group, dispatch_get_main_queue(), {
        if (knownBeacons.count > 0) {
            self.addToFoundBeacons(knownBeacons)
        }
    })

    tableView.reloadData()
}

    func addToFoundBeacons(knownBeacons: [AnyObject]!) {
    for i in 0..<knownBeacons.count {
        var isOnList = false
        let foundBeacon = knownBeacons[i] as! CLBeacon
        for j in 0..<Equipments.count {
            if (knownBeacons[i].minor == Equipments[j].minor) {
                isOnList = true
            }
        }
        if (isOnList == false) {
            httpProtocol.makeGetRequestForEquipment(foundBeacon.minor) { equipment in
                if let equipment = equipment {
                    self.Equipments.append(equipment)
                }
            }
        }
    }
}

func deleteFromFoundBeacons(knownBeacons: [AnyObject]!) {
    for i in 0..<Equipments.count {
        var isToDelete = true
        for j in 0..<knownBeacons.count {
            if (knownBeacons[j].minor == Equipments[i].minor) {
                isToDelete = false
            }
        }
        if (isToDelete) {
            Equipments.removeAtIndex(i)
        }
    }
}

向设备(和表格视图)添加新信标可以正常工作,但是当任何信标离开扫描范围时,就会出现错误。我的问题是如何使信标管理独立于每秒钟调用一次的 locationManager 函数?

这些是tableView的方法:

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return Equipments.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("EquipmentTableViewCell", forIndexPath: indexPath) as! EquipmentTableViewCell

    let equipment = Equipments[indexPath.row]

    cell.equipmentLabel.text = equipment.name
    cell.equipmentPhoto.image = equipment.photo

    return cell
}

【问题讨论】:

  • 您能否展示您的方法实现,这些实现返回 tableView 中的项目数和每个中的特定项目?
  • @davidgyoung 查看编辑后的帖子
  • 为什么代码在调用addToFoundBeacons之前调用deleteFromFoundBeacons?
  • @davidgyoung 我想在新扫描后首先从表视图中删除超出范围的信标,然后添加扫描范围内的新信标。可能这个方法的名字不好,但是这个方法会从 Equipments 中删除所有在实际扫描中没有找到 minor 的对象。

标签: json swift asynchronous ibeacon alamofire


【解决方案1】:

通过在主循环的每次迭代之前添加另一个 if 语句来检查 i &lt; Equipments.count 是否在 deleteFromFoundBeacons 中解决了这个问题。多亏了它,我还摆脱了locationManager 中的调度部分。我还将tableView.reloadData() 更改为beginUpdates() - endUpdates() 部分,以便在每次扫描中动态管理表。

【讨论】:

    【解决方案2】:

    我怀疑您遇到了异常,因为deleteFromFoundBeacons 中的代码正在从循环中的数组中删除一个项目,该循环对其进行迭代。你不能那样做。您可能需要使用所需的信标构造一个新数组,然后在最后用新数组替换旧数组。

    【讨论】:

    • 我没有考虑过。当我有机会时,我会检查它。我认为这应该可行。
    猜你喜欢
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 2019-02-24
    相关资源
    最近更新 更多