小编lin*_*ram的帖子

Swift默认AlertViewController突破约束

我正在尝试使用样式为.actionSheet的默认AlertViewController。由于某种原因,警报导致约束错误。只要没有通过按钮触发(显示)alertController,整个视图就不会出现约束错误。难道这是Xcode错误?

我得到的确切错误如下所示:

2019-04-12 15:33:29.584076+0200 Appname[4688:39368] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x6000025a1e50 UIView:0x7f88fcf6ce60.width == - 16   (active)>"
)

Will attempt to recover by breaking …
Run Code Online (Sandbox Code Playgroud)

iphone xcode ios swift ios-autolayout

37
推荐指数
4
解决办法
2232
查看次数

在多个数据库上使用 Mongoose-Transactions

我正在创建一个由多个微服务组成的 Node.js API。

每个微服务负责我的应用程序的一个或多个功能。但是,我的数据被构建到多个数据库中,每个数据库都有多个集合。

现在我需要一项服务来跨多个数据库执行原子操作。如果一切都发生在同一个数据库中,我会使用普通事务。但是,我不知道如何对多个数据库执行此操作,或者这是否可能?

示例

微服务之一负责创建用户。必须在两个数据库中创建一个用户。但是,这应该自动发生,即如果创建了用户,则必须在两个数据库中都创建它。

更新MongoDB 的官方文档声明如下:

通过分布式事务,事务可以跨多个操作、集合、数据库、文档和分片使用。

不过,我还没有找到任何关于如何使用 mongoose 执行分布式事务的信息。

如果有人能给我一些关于这个主题的澄清,我会非常高兴。

transactions mongoose mongodb nosql node.js

6
推荐指数
1
解决办法
971
查看次数

在多个微服务上使用猫鼬模式

我的应用程序被分成多个在heroku dynos上运行的微服务(它们无法访问彼此的文件)。有时,有多个微服务与一个集合一起使用。因此,这两个微服务都需要相应的猫鼬模式。

但是,并非两个微服务都需要完整架构。例如,微服务 A 需要完整架构,而微服务 B 仅需要该架构的几个字段。

微服务 A 内的示例架构:

var AccountSchema = mongoose.Schema({
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    phone: { type: String, required: true, unique: true },
    forename: { type: String, required: true },
    surname: { type: String, required: true },
    middleInitals: { type: String, required: false },
    failedLoginAttempts: { type: Number, required: true, default: 0 },
    lockUntil: { type: Number },
    createdAt: …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js microservices mongoose-schema

5
推荐指数
1
解决办法
1826
查看次数

Typescript 和 Mongoose - “this”在实例方法中不可用

我目前正在将我的 API 从 JS 转换为 TS。然而,我在猫鼬和打字稿方面遇到了一些困难。具体来说,this在我的实例方法中不可用。

我的代码:

AccountSchema.methods.comparePassword = async function (candidatePassword: string) {
  const isMatch = await bcrypt.compare(candidatePassword, this.password);
  return isMatch;
};
Run Code Online (Sandbox Code Playgroud)

this当引用函数而不是实际文档时,它会生成以下错误:

“函数”类型的参数不可分配给“字符串”类型的参数。

但是,根据Mongoose 的文档,不应该有错误,this应该参考实际文档。

这实际上指的是:

this: {
    [name: string]: Function;
}
Run Code Online (Sandbox Code Playgroud)

我如何定义我的架构:

const AccountSchema = new mongoose.Schema<IAccount>({...})
Run Code Online (Sandbox Code Playgroud)

我的方法与 JS 配合得很好,所以我知道它与 TypeScript 有关。Mongoose 文档确认我的实现是定义实例方法的正确方法,所以我很困惑为什么它不起作用。

是否有我不知道的在猫鼬中声明和使用实例方法的特定 TS 方式?

任何帮助将不胜感激!

mongoose node.js typescript

5
推荐指数
1
解决办法
1423
查看次数

如何使 SwiftUI ViewBuilder 只接受特定协议的视图作为输入块?

我正在尝试使用 SwiftUI@ViewBuilder来允许我的包的用户动态指定视图的正文内容。但是,我试图将可用作输入的可能视图限制为我实现的几个默认视图(出于简化目的)。

这是一个例子:

public protocol PopupBodyView: View { } // every one of my default views conforms to this protocol

func generateChildView(@ViewBuilder items: () -> PopupBodyView)
-> some PopupBodyView {
    return menuItems()
}
Run Code Online (Sandbox Code Playgroud)

如果我现在调用generateChildView,我可以正确指定协议类型的一种PopupBodyView视图- 但是,如果我在结果生成器主体中指定多个视图,则会收到以下错误:

Instance method 'contextMenu(items:)' requires that 'TupleView<(Text, Text)>' conform to 'PopupBodyView'

extension Text: PopupBodyView { } // add protocol conformance

contextMenu {
    Text("Test")
    Text("Test") // Instance method 'contextMenu(items:)' requires that 'TupleView<(Text, Text)>' conform to 'PopupBodyView'
}
Run Code Online (Sandbox Code Playgroud)

我现在如何调整默认值@ViewBuilder以仅接受符合我的自定义 …

generics swift swift-protocols swiftui viewbuilder

5
推荐指数
0
解决办法
350
查看次数

尽管有 @MainActor 注释,但主线程上未触发 UI 更新

我正在注释我的函数,@MainActor以确保可以从任何异步位置安全地调用它以触发 UI 更新。尽管如此,我还是遇到了一个错误,不知何故,UI 更新似乎是在后台线程上尝试的,即使(据我理解)该函数严格绑定到`@MainActor\xc2\xb4.

\n

这是我的代码:

\n
/// Dismisses the popup from its presenting view controller.\n@MainActor public func dismiss() {\n    presentingViewController?.dismiss(self)\n}\n
Run Code Online (Sandbox Code Playgroud)\n

它是从 an 内部调用的,NSViewController它使用 监听某个事件NotificationCenter,然后在以下objc函数中启动解雇:

\n
class MainWindowControllerVC: NSWindowController, NSWindowDelegate {\n  override func windowDidLoad() {\n      NotificationCenter.default.addObserver(self, selector: #selector(self.dismissVCastSharePopup), name: .NOTIF_DISMISS_POPUP, object: nil)\n  }\n\n  @objc private func dismissPopup() {\n      // some other cleanup is happening here\n      popup?.dismiss()\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我收到以下错误:

\n
*** Terminating app due to uncaught exception \'NSInternalInconsistencyException\', reason: \'NSWindow drag …
Run Code Online (Sandbox Code Playgroud)

macos appkit actor swift swift-concurrency

4
推荐指数
1
解决办法
882
查看次数

如何在 Swift 中使函数的返回类型通用

介绍

在我的应用程序中,我有一个名为“ElementData”的超类几个继承自它的子类。

每个子类都有自己的 validateModel() 方法,它返回不同的类型,具体取决于类——总是在一个数组中。

换句话说:该方法在每个子类中返回不同的类型。

例子

A类: func validateModel() -> [String]

B类: func validateModel() -> [Int]

C类: func validateModel() -> [MyCustomEnum]

如您所见,只有返回值彼此不同。

编辑:validateModel() 方法示例:

A类:

func validateModel() -> [DefaultElementFields]{ // DefaultElementFields is an enum with the different view types of my collection view

        var checkResult: [DefaultElementFields] = []

        if name == "" {
            checkResult.append(.Name)
        }

        if Int(rewardedPoints) == nil {
            checkResult.append(.Points)
        }

        if description == "" {
            checkResult.append(.Description)
        }

        if selectedImage == nil {
            checkResult.append(.Image) …
Run Code Online (Sandbox Code Playgroud)

arrays class generic-programming swift

2
推荐指数
2
解决办法
3551
查看次数

Mongoose 将数据从 withTransaction 助手中传递出去

介绍

嘿,

我正在尝试从猫鼬withTransaction回调中传递数据。现在,我正在使用以下实现回调的代码:

const transactionSession = await mongoose.startSession()
await transactionSession.withTransaction(async (tSession) => {
    try {
        // MARK Transaction writes & reads removed for brevity

        console.log("Successfully performed transaction!")
        cb(null, "Any test data")

        return Promise.resolve()
    } catch (error) {
        console.log("Transaction aborted due to error:", error)
        cb(error)

        return Promise.reject()
    }
})

} catch (error) {
    console.log(error)
    return cb(error)
}
Run Code Online (Sandbox Code Playgroud)

withTransaction可以在此处找到正在使用的帮助程序的更详细片段。

withTransaction可以在此处找到有关帮助程序的官方 Mongoose 文档的链接。

目前,我正在使用回调从回调中传递数据withTransaction

cb(null, "Any test data")

然而,问题是自然而然地在Promise.resolve()返回之前首先执行回调。这意味着(在我的情况下)在提交任何必要的数据库写入之前将成功响应发送回客户端:

// this …
Run Code Online (Sandbox Code Playgroud)

javascript mongoose mongodb node.js async-await

2
推荐指数
1
解决办法
1867
查看次数

在 javascript 中使用正则表达式验证用户名

我是正则表达式的新手,想创建一个正则表达式来检查用户名。这些是条件:

  1. 用户名必须包含 4 到 20 个字符
  2. 用户名不能包含除字母 az、数字 0-9 和特殊字符 -._ 之外的任何内容
  3. 特殊字符 -._ 不得连续使用以避免混淆
  4. 用户名不得包含空格

例子

  • 任何.user.13 => 有效
  • any..user13 => 无效(连续两个点)
  • 任何用户=> 有效
  • 任何=> 无效(太短)
  • anyuserthathasasupersuperlonglongname => 无效(字符太多)
  • 任何用户名=> 由于空格而无效

我尝试创建自己的正则表达式,但只指定了允许的字符:

[a-z0-9.-_]{4,20}
Run Code Online (Sandbox Code Playgroud)

不幸的是,如果中间有空格,它仍然匹配字符串,并且可能连续有两个特殊字符 .-_ :

尽管连续匹配空格和两个点

如果有人能够在这个问题上为我提供帮助,我将不胜感激。请记住,我是正则表达式的新手,仍在学习中。因此,对你的正则表达式的解释会很棒。

提前致谢 :)

javascript regex

1
推荐指数
1
解决办法
2642
查看次数