Core Data, für mich nicht erklärlicher Fehler

    Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

    • Core Data, für mich nicht erklärlicher Fehler

      Hallo Zusammen,

      ich bin mal wieder am kämpfen mit Core Data. Ich weiss zwar, dass CD keine Datenbank ist, aber ein Filter über Datensätze zu legen, sollte eigentlich schon möglich sein. Hier mal der Code, für sachdienliche Hinweise bin ich dankbar.


      Quellcode

      1. //das funktioniert soweit
      2. let ctx = getPersistentContainer()!.viewContext
      3. let request = NSFetchRequest<Tab>(entityName: "Tab")
      4. //hier bricht er teilweise ab, wenn das array [key] nicht vorhanden ist.
      5. //wenn es vorhanden ist, und er zufällig nicht abbricht, dann findet er leider nichts
      6. request.predicate = NSPredicate(format: "key = %@", [key])
      7. //wenn er nicht abbricht, vorherige Programmzeile, dann bricht er in dieser Progrmmzeile ab :(
      8. var tab = try? ctx.fetch(request)
      Hat einer eine Idee, was an diesem Fetch oder Predicate falsch ist? Ich bin mit meinen Latein am Ende.

      Wolf
    • Hallo Zusammen,

      er bringt keine Fehlermeldung, sondern die App stürzt direkt ab. Im Debugger kommt dann die folgende Meldung: "Thread 1: EXC_BAD_ACCESS (code=1, address=0x2)"

      Das mit dem Array, war ein Versuch das Problem zu umgehen. beim 1. Predicate funktionierte es sogar, aber dann beim 2. krachte es. Wenn ich das Array wegnehme, dann kracht es gleich beim 1. Predicate

      Hat noch jemand eine Idee?

      Schöne Grüsse
      Wolf
    • Hi Mattes,

      eigentlich nicht. In der Doc steht bspw. folgendes:
      Spoiler anzeigen
      Documentation ArchiveDeveloper

      Predicate Programming Guide

      NextPrevious
      Predicate Format String Syntax

      This article describes the syntax of the predicate string and some aspects of the predicate parser.
      The parser string is different from a string expression passed to the regex engine. This article describes the parser text, not the syntax for the regex engine.
      Parser Basics
      The predicate string parser is whitespace insensitive, case insensitive with respect to keywords, and supports nested parenthetical expressions. The parser does not perform semantic type checking.
      Variables are denoted with a dollar-sign ($) character (for example, $VARIABLE_NAME). The question mark (?) character is not a valid parser token.
      The format string supports printf-style format specifiers such as %x (see Formatting String Objects). Two important format specifiers are %@ and %K.
      • %@ is a var arg substitution for an object value—often a string, number, or date.
      • %K is a var arg substitution for a key path.
      When string variables are substituted into a string using the %@ format specifier, they are surrounded by quotation marks. If you want to specify a dynamic property name, use %K in the format string, as shown in the following example.
      NSString *attributeName = @"firstName";
      NSString *attributeValue = @"Adam";
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",
      attributeName, attributeValue];

      The predicate format string in this case evaluates to firstName like "Adam".
      Single or double quoting variables (or substitution variable strings) cause %@, %K, or $variable to be interpreted as a literal in the format string and so prevent any substitution. In the following example, the predicate format string evaluates to firstName like "%@" (note the single quotes around %@).
      NSString *attributeName = @"firstName";
      NSString *attributeValue = @"Adam";
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like '%@'",
      attributeName, attributeValue];

      Important: Use a %@ format specifier only to represent an expression. Do not use it to represent an entire predicate.
      If you attempt to use a format specifier to represent an entire predicate, the system raises an exception.
      Basic Comparisons
      =, ==The left-hand expression is equal to the right-hand expression.
      >=, =>The left-hand expression is greater than or equal to the right-hand expression.
      <=, =<The left-hand expression is less than or equal to the right-hand expression.
      >The left-hand expression is greater than the right-hand expression.
      <The left-hand expression is less than the right-hand expression.
      !=, <>The left-hand expression is not equal to the right-hand expression.


      Predicate Format String Syntax

      Das sollte also nichts ausmachen.

      Schöne Grüsse
      Wolf
    • Hi Matthes und die anderen,

      nein, er stürzt hier direkt ab, ohne eine Fehlermeldung. key ist ein Integer, daher kann hier like nicht in Frage kommen.

      1. Quellcode

        1. let x = NSPredicate(format: "Key == %@", key)



      Irgendwie, mag er den NSPredicate nicht. Wenn ich nur wüsste, an was es liegen kann..

      Grüsse
      Wolf
    • Wolf schrieb:

      key ist ein Integer, daher kann hier like nicht in Frage kommen.
      Also in Objective-C würde bei Abfrage eines Integer-Wertes das Predicate aber eher wie NSPredicate *predicate = [NSPredicate predicateWithFormat:@"integerAttribute == %lu", myInteger]; lauten. Sicher, dass "%@" in dem Format-String die richtige Idee ist...?

      Mattes (ein nicht-Swifty)
      Diese Seite bleibt aus technischen Gründen unbedruckt.
    • Wolf schrieb:

      Hi Matthes und die anderen,

      nein, er stürzt hier direkt ab, ohne eine Fehlermeldung. key ist ein Integer, daher kann hier like nicht in Frage kommen.

      1. Quellcode

        1. let x = NSPredicate(format: "Key == %@", key)


      Also wenn Key ein Integer ist, dann müsste es eigentlich key = %d heißen. Oder sind Format Strings beim Predicate egal?

      Oh Mattes hat es ja schon erfasst. :D

      Habe es gerade mal in meiner App getestet. Pass deinen Format-String an, dann sollte es klappen.

      Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von Kaizen ()

    • %@ ist nicht per-se falsch. Es ist aber auch nicht egal, weil %@ etwa bei Strings escaped. Deshalb gibt es ja %K für Keys. Ich nehme an, dass es funktioniert, wenn das übergebene Objekt eine Instanz von NSNumber ist. (Das sollte jedenfalls so sein.)
      Es hat noch nie etwas gefunzt. To tear down the Wall would be a Werror!
      25.06.2016: [Swift] gehört zu meinen *Favorite Tags* auf SO. In welcher Bedeutung von "favorite"?