Thread-Laufzeit für Delegates verlängern

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

  • Thread-Laufzeit für Delegates verlängern

    Hallo Leute,

    ich erstelle in einer FOR-Schleife einen ganzen Haufen Instanzen einer Klasse. Da das so seine Zeit dauert mache ich das ganze in einem eigenen Thread, den ich folgendermaßen aufrufe:

    Quellcode

    1. NSThread* myThread = [[NSThread alloc] initWithTarget:self
    2. selector:@selector(createObjects)
    3. object:nil];
    4. [myThread start];



    Das ganze scheint auch wunderbar zu funktionieren. Bis auf die Ausnahme, dass es nur selten oder gar nicht dazu kommt, dass bestimmte Delegate Methoden der besagten Klasse aufgerufen werden. Meine Vermutung ist, dass der Thread einfach schon abgeschlossen ist, wenn die Delegate Methoden normalerweise aufgerufen werden.

    Frage: Was kann ich tuen, dass dieser Theras etwas länger existiert? 5 Sekunden würden schon völlig reichen.

    Hat jemand eine Idee?

    Vielen Dank und Gruß
    Daniel
    “I want to see an elephant hunt down a man for the sole purpose of collecting his teeth, while a chorus of typewriters sings songs that praises the bananas for their wisdom, leadership, and their high levels of potassium.” ― Jarod Kintz, I Want
  • Wie mache ich das dann mit dem selector?

    Meine Delegate Methode in der Klasse heißt - (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet

    ist übrigens von hier: developer.apple.com/library/ma…g/Introduction/Intro.html
    “I want to see an elephant hunt down a man for the sole purpose of collecting his teeth, while a chorus of typewriters sings songs that praises the bananas for their wisdom, leadership, and their high levels of potassium.” ― Jarod Kintz, I Want
  • ich würde es dann so machen

    im thread:

    Quellcode

    1. MyClass *obj;
    2. // alles wird erstellt bla bla ...
    3. // dann wenn du alles hast sagst du
    4. [obj performSelectorOnMainThread:@selector(_callDelegate:) withObject:@"I'm a parameter" waitUntilDone:YES];


    in MyClass gibt es eine Methode die _callDelegate heißt:

    Quellcode

    1. - (void) _callDelegate:(id)arg {
    2. if ([_delegate respondsToSelector:@selector(simplePing:didReceivePingResponsePacket:)]) {
    3. [_delegate simplePing:self didReceivePingResponsePacket:self.myData];
    4. }
    5. }


    wobei ob du ein Objekt mitübergibst, bei performSelectorOnMainThread, ist dir überlassen. kannst auch nil hinschicken.

    PS: ein @selector ist nix anderes als ein methodenname :)
    俺の世界にようこそ
  • Danke schon mal so weit.

    Allerdings habe ich noch ein Problem:

    Ich weiß nicht was ich als Parameter bei der Delegate Methode übergeben soll:

    Quellcode

    1. - (void)_callDelegate:(id)arg{
    2. if ([self respondsToSelector:@selector(simplePing:didReceivePingResponsePacket:)]) {
    3. [self simplePing:_pinger didReceivePingResponsePacket:@"KEIN PLAN WAS HIER HIN SOLL"];
    4. }
    5. }


    Dieses Packet kommt ja von der SimplePing Klasse. Hmmm

    Im Anhang mal die Klasse über die wir sprechen, die auch die Delegates hat:
    osxentwicklerforum.de/index.ph…f057134999dd06a1db3843b9f
    (Achtung: Ist noch etwas unaufgeräumt und so ;) )

    Edit: Irgendwie rutscht der jetzt auch bei jeder Instanz in die if Schleife von _callDelegate.
    “I want to see an elephant hunt down a man for the sole purpose of collecting his teeth, while a chorus of typewriters sings songs that praises the bananas for their wisdom, leadership, and their high levels of potassium.” ― Jarod Kintz, I Want

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

  • darf ich fragen ob das ein lernprojekt ist? :)
    wenn ja finde ich, ist das ein bischen viel für den anfang.

    außerdem warum erzeugst du ewig viele instanzen hintereinander? wenn das ein PingProgramm ist dann sollte doch ein Rekursiver aufruf reichen, oder?

    und zum thema delegates, guck dir mal das bitte an :) cocoadevcentral.com/articles/000075.php und das iphonedevelopertips.com/object…tocols-and-delegates.html
    俺の世界にようこそ
  • Ja irgendwie ist das schon ein Lernprojekt, wo aber auch was sinnvolles bei rauskommen soll.

    Ich erzeuge so viele Instanzen hintereinander, weil ich eben eine ganze Liste von Geräten anpingen möchte. Der Ping Teil ist auch nur ein Teil eines ganzen.

    Ich schau mir noch mal die Links an. Vielleicht bekomme ich's dann hin. Danke"
    “I want to see an elephant hunt down a man for the sole purpose of collecting his teeth, while a chorus of typewriters sings songs that praises the bananas for their wisdom, leadership, and their high levels of potassium.” ― Jarod Kintz, I Want
  • @ Claus: Also du meinst, dass ich quasi nicht über eine zweite Klasse (wie jetzt) die mit Delegates auf die SImplePing Klasse reagiert, sondern direkt die SimplePing Klasse abändere...

    Hab mir schon gedacht, dass das so besser wäre, es war so nur einfacher (weil fast 1 zu 1 übernehmen aus dem SimplePing Beispielprojekt). :thumbsup:

    Edit: Allerdings hätte ich dann vermutlich auch das Problem, dass der Thread schon abgelaufen ist, wenn SimplePing die Antwort bekommt. Naja, ich muss mir Simple Ping mal ganz genau anschauen.
    “I want to see an elephant hunt down a man for the sole purpose of collecting his teeth, while a chorus of typewriters sings songs that praises the bananas for their wisdom, leadership, and their high levels of potassium.” ― Jarod Kintz, I Want

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

  • Wieso hat mir niemand was von NSOperation und NSOperationQueue gesagt? Damit funktioniert es wunderbar :)
    “I want to see an elephant hunt down a man for the sole purpose of collecting his teeth, while a chorus of typewriters sings songs that praises the bananas for their wisdom, leadership, and their high levels of potassium.” ― Jarod Kintz, I Want