【问题标题】:How to limit function to once in every x SWIFT如何将功能限制为每 x SWIFT 一次
【发布时间】:2021-08-19 04:48:33
【问题描述】:

所以我在我的应用上创建了一个 cmets 选项,让用户可以对帖子发表评论。它被直接引用并将信息从字符串发送到 api 端点。这行得通,现在我想知道是否有办法限制对 API 的基本请求数量,以便用户可以每分钟发送一次 cmets

    TextField("Comment...", text: $comment) { editingChanged in
                    } onCommit: {
                        validate()
                        showsAlert = !isValid
                        if isValid{
                            viewModel.sendComment(nickname: nickname, body: comment) {
                                self.comment = ""
                            }
                        }
                    }
                }
                .padding()
                .frame(width: UIScreen.main.bounds.width, height: 80)
                .ignoresSafeArea(.keyboard, edges: .bottom)
                

这将帮助我减少 cmets 上的垃圾邮件,并且可以挽救生命

【问题讨论】:

    标签: json swift api swiftui uikit


    【解决方案1】:

    您可以创建一个简单的 Swift 类来控制时间。

    public class Time_Control:Thread{
    
        var wait_time:Int //In seconds
        public var can_send:Bool = true
    
        init(_ wait_time:Int) {
            self.wait_time = wait_time
        }
    
        public override func start() {
            super.start()
    
            self.can_send = false
            Thread.self.sleep(forTimeInterval: TimeInterval(self.wait_time))
            self.can_send = true
        }
    }
    

    然后您可以在代码的任何部分使用此对象来控制事件之间的时间。为此,首先要有一个变量来存储类Time_Control 的对象,例如var control:Time_Control = Time_Control(0)。之后,每次您想控制某事的时间时,只需执行以下操作:

    func send_message(){
        if control.can_send{
            //Start the time controller with 60 seconds
            control = Time_Control(60)
            control.start()
    
            //Let the user send message
            //... your code
        }else{
            //Don't let the user send message
            //...
        }
    }
    

    当用户与您的 UI 交互时,您可以调用函数send_message()。它将允许用户每 60 秒发送一条消息。

    【讨论】:

      【解决方案2】:

      在 Swift 中,您可以使用以下方法获取当前时刻的 Unix 时间戳:

      let currentUnixTimestamp = Date().timeIntervalSince1970
      

      Unix 时间仅计算自 1970 年 1 月 1 日 00:00:00 UTC 以来经过的秒数。请参阅Wikipedia 了解更多信息。

      使用它,您可以创建一个字段,每次发送评论时保存当前时间戳,然后根据保存的时间戳检查当前时间。如果您的viewModel 属于类类型(我认为是),那么这是放置此代码的最佳位置。它可能看起来像这样:

      class ViewModel {
          // How many seconds to wait before the user can comment again
          private static let cooldownInSeconds = 60
      
          private var lastSendTimestamp: Int? = nil
      
      
          func sendComment(nickname: String, body: String) {
              let currentTime = Date().timeIntervalSince1970
      
              if currentTime >= (lastSendTimestamp ?? Int.min) + ViewModel.cooldownInSeconds {
                  lastSendTimestamp = currentTime
                  
                  // Send comment here
              } else {
                  // Cannot send comment because cooldown has not run out
              }
          }
      }
      

      请注意,将变量放入 structs(如您的 View)将无法正常工作,因为它们是不可变的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-13
        • 2018-08-26
        • 2021-09-21
        • 2012-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多